Class: Inch::CLI::CommandParser
- Inherits:
-
Object
- Object
- Inch::CLI::CommandParser
- Includes:
- TraceHelper
- Defined in:
- lib/inch/cli/command_parser.rb
Overview
CommandParser parses a command-line arguments to decide which Command to run.
The basic form translates this shell command
$ inch command_name [options]
into a method call on the corresponding Command class.
Some examples:
$ inch
# >>> Command::Suggest.new.run()
$ inch --pedantic
# >>> Command::Suggest.new.run("--pedantic")
As you can see, if no command_name is given, the CommandParser.default_command will be used.
$ inch list --all
# >>> Command::List.new.run("--all")
If a command_name is found to match a Command, that Command will be used.
$ inch --help
# >>> CommandParser#list_commands
The --help
switch is an exception and lists all available commands.
Class Attribute Summary collapse
-
.commands ⇒ Hash{Symbol => Command}
The mapping of command names to command classes to parse the user command.
-
.default_command ⇒ Symbol
The default command name to use when no options are specified or.
Class Method Summary collapse
-
.run(*args) ⇒ Command::Base
Convenience method to create a new CommandParser and call #run.
Instance Method Summary collapse
-
#run(*args) ⇒ Command::Base
Runs the Command object matching the command name of the first argument.
Methods included from TraceHelper
Class Attribute Details
.commands ⇒ Hash{Symbol => Command}
Returns the mapping of command names to command classes to parse the user command.
39 40 41 |
# File 'lib/inch/cli/command_parser.rb', line 39 def commands @commands end |
.default_command ⇒ Symbol
Returns the default command name to use when no options are specified or.
43 44 45 |
# File 'lib/inch/cli/command_parser.rb', line 43 def default_command @default_command end |
Class Method Details
.run(*args) ⇒ Command::Base
Convenience method to create a new CommandParser and call #run
50 51 52 |
# File 'lib/inch/cli/command_parser.rb', line 50 def self.run(*args) new.run(*args) end |
Instance Method Details
#run(*args) ⇒ Command::Base
Runs the Inch::CLI::Command object matching the command name of the first argument.
57 58 59 60 61 62 63 |
# File 'lib/inch/cli/command_parser.rb', line 57 def run(*args) if ['--help', '-h'].include?(args.join) list_commands else run_command(*args) end end |