Module: Fronde::CLI::OptParse
- Defined in:
- lib/fronde/cli/opt_parse.rb
Overview
Helper code to help build the fronde option parser
Constant Summary collapse
- FRONDE_OPTIONS =
Returns the possible ~fronde~ options and their configuration.
{ '-a' => { long: 'author' }, '-f' => { long: 'force', boolean: true }, '-h' => { long: 'help', boolean: true, method: :on_tail, help: I18n.t('fronde.bin.options.help') }, '-l' => { long: 'lang', keyword: 'LOCALE' }, '-o' => { long: 'output', keyword: 'FORMAT', choices: %w[gemini html] }, '-t' => { long: 'title' }, '-v' => { long: 'verbose', boolean: true, method: :on_tail }, '-V' => { long: 'version', boolean: true, method: :on_tail, help: I18n.t('fronde.bin.options.version') } }.freeze
- FRONDE_COMMANDS =
Returns the possible ~fronde~ subcommands and their configuration.
{ 'new' => { opts: ['-a', '-l', '-o', '-t', '-v'], label: 'new <path>' }, 'init' => { alias: 'new' }, 'update' => { opts: ['-v'] }, 'config' => { alias: 'update' }, 'preview' => {}, 'open' => { opts: ['-a', '-l', '-t'], label: 'open <path>' }, 'edit' => { alias: 'open' }, 'build' => { opts: ['-f', '-v'] }, 'publish' => { opts: ['-v'] }, 'help' => {}, 'basic' => { opts: ['-h', '-V'], label: '<command>' } }.freeze
Class Method Summary collapse
-
.command_options(command) ⇒ Hash
Returns the given command options.
-
.decorate_option(short) ⇒ Array
Returns the short and long options specification for a given short option.
- .help_command_body(command) ⇒ Object
-
.list_commands ⇒ String
Returns a formatted list of available commands for ~fronde~.
-
.resolve_possible_alias(command) ⇒ String
Returns the real command name for a given command, which may be an alias.
-
.summarize_command(command) ⇒ String
Returns the ~fronde~ help summary for a given command.
Class Method Details
.command_options(command) ⇒ Hash
Returns the given command options.
This method will first try to resolve command alias, if any.
133 134 135 136 |
# File 'lib/fronde/cli/opt_parse.rb', line 133 def (command) cmd = resolve_possible_alias command FRONDE_COMMANDS[cmd].merge(name: cmd) end |
.decorate_option(short) ⇒ Array
Returns the short and long options specification for a given
short option.
This method use the FRONDE_OPTIONS Hash to retrieve corresponding values.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fronde/cli/opt_parse.rb', line 55 def decorate_option(short) opt = FRONDE_OPTIONS[short] long = "--#{opt[:long]}" if opt[:boolean] config = [short, long] else key = opt[:keyword] || opt[:long].upcase config = [short, format('%<long>s %<key>s', long:, key:)] end config.push opt[:choices], opt[:help] config.compact end |
.help_command_body(command) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fronde/cli/opt_parse.rb', line 85 def help_command_body(command) command_opts_doc = summarize_command(command) return '' if command_opts_doc == '' body = [I18n.t('fronde.bin.options.cmd_title'), command_opts_doc] if command == 'basic' body += ['', I18n.t('fronde.bin.commands.cmd_title'), list_commands] end body.join("\n") end |
.list_commands ⇒ String
Returns a formatted list of available commands for ~fronde~.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fronde/cli/opt_parse.rb', line 99 def list_commands FRONDE_COMMANDS.filter_map do |cmd, opt| next if cmd == 'basic' line = [' ', cmd.ljust(10)] if opt.has_key? :alias line << I18n.t('fronde.bin.commands.alias', alias: opt[:alias]) else line << I18n.t("fronde.bin.commands.#{cmd}") end line.join(' ') end.join("\n") end |
.resolve_possible_alias(command) ⇒ String
Returns the real command name for a given command, which may be
an alias.
118 119 120 121 122 123 124 125 |
# File 'lib/fronde/cli/opt_parse.rb', line 118 def resolve_possible_alias(command) return 'basic' unless FRONDE_COMMANDS.include?(command) cmd_opt = FRONDE_COMMANDS[command] return cmd_opt[:alias] if cmd_opt.has_key?(:alias) command end |
.summarize_command(command) ⇒ String
Returns the ~fronde~ help summary for a given command.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fronde/cli/opt_parse.rb', line 73 def summarize_command(command) (FRONDE_COMMANDS[command][:opts] || []).map do |k| short, long = decorate_option(k) opt = FRONDE_OPTIONS[k] label = [short, long].join(', ') line = [format(' %<opt>s', opt: label).ljust(30), opt[:help]] choices = opt[:choices] line << "(#{choices.join(', ')})" if choices line.compact.join(' ') end.join("\n") end |