Class: CodePicture::Cli
- Inherits:
-
Object
- Object
- CodePicture::Cli
- Defined in:
- lib/code_picture/cli.rb,
lib/code_picture/cli/options.rb,
lib/code_picture/cli/commands/help.rb,
lib/code_picture/cli/commands/version.rb,
lib/code_picture/cli/commands/take_picture.rb,
lib/code_picture/cli/commands/result/failure.rb,
lib/code_picture/cli/commands/result/success.rb
Defined Under Namespace
Modules: Commands
Constant Summary collapse
- Options =
Data.define(:command, :command_options) do def self.empty = new(command: nil, command_options: Commands::TakePicture::Options.default) def self.from(argv) = Cli::Options.empty opt_parser = OptionParser.new do |parser| parser. = "Usage: code-picture path-to-file.rb [options]" parser.on("-p", "--pixel-size=SIZE", Integer, "Define the pixel size of the generated image") do |size| = ..with(pixel_size: size) = .with(command_options:) end parser.on("-r", "--max-pixels-per-row=SIZE", Integer, "Define the maximum number of pixels per row") do |size| = ..with(max_pixels_per_row: size) = .with(command_options:) end parser.on("-t", "--theme=THEME", String, "Define the theme of the generated image [options: one-dark-pro (default), random, or a path to a YAML file]") do |theme| = ..with(theme: theme) = .with(command_options:) end parser.on("-o", "--output=FILE", String, "Write the generated image to the given file path") do |file| = ..with(output_file_path: file) = .with(command_options:) end parser.on("-v", "--version", "Displays app version") do = .with(command: Commands::Version, command_options: nil) end parser.on("-h", "--help", "Prints this help") do = Cli::Options.new( command: Commands::Help, command_options: Commands::Help::Options.new(help_text: parser.to_s) ) end end begin opt_parser.parse!(argv) rescue OptionParser::InvalidOption = Cli::Options.new( command: Commands::Help, command_options: Commands::Help::Options.new(help_text: opt_parser.to_s) ) end if (input_file_path = argv.first) .with( command: Commands::TakePicture, command_options: ..with(input_file_path:) ) elsif .command.nil? .with( command: Commands::Help, command_options: Commands::Help::Options.new( error: "Missing input file path", help_text: opt_parser.to_s ) ) else end end end
Instance Method Summary collapse
Instance Method Details
#call(argv) ⇒ Object
5 6 7 8 9 |
# File 'lib/code_picture/cli.rb', line 5 def call(argv) Options .from(argv) .then { _1.command.call(_1.) } end |