Class: MisterBin::Command
- Inherits:
-
Object
- Object
- MisterBin::Command
- Includes:
- Colsole
- Defined in:
- lib/mister_bin/command.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
- .command(name, text) ⇒ Object
- .docopt ⇒ Object
- .environment(name, value) ⇒ Object
- .example(text) ⇒ Object
- .execute(argv = []) ⇒ Object
- .find_target_command(instance, args) ⇒ Object
- .help(text) ⇒ Object
- .meta ⇒ Object
- .option(flags, text) ⇒ Object
- .param(param, text) ⇒ Object
-
.summary(text) ⇒ Object
DSL.
- .target_commands ⇒ Object
- .usage(text) ⇒ Object
- .version(text) ⇒ Object
Instance Method Summary collapse
- #execute(argv = []) ⇒ Object
-
#initialize(args = nil) ⇒ Command
constructor
A new instance of Command.
Constructor Details
#initialize(args = nil) ⇒ Command
Returns a new instance of Command.
10 11 12 |
# File 'lib/mister_bin/command.rb', line 10 def initialize(args = nil) @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
8 9 10 |
# File 'lib/mister_bin/command.rb', line 8 def args @args end |
Class Method Details
.command(name, text) ⇒ Object
51 52 53 54 |
# File 'lib/mister_bin/command.rb', line 51 def command(name, text) target_commands << name.to_sym .commands << [name, text] end |
.docopt ⇒ Object
72 73 74 |
# File 'lib/mister_bin/command.rb', line 72 def docopt .docopt end |
.environment(name, value) ⇒ Object
64 65 66 |
# File 'lib/mister_bin/command.rb', line 64 def environment(name, value) .env_vars << [name, value] end |
.example(text) ⇒ Object
60 61 62 |
# File 'lib/mister_bin/command.rb', line 60 def example(text) .examples << text end |
.execute(argv = []) ⇒ Object
25 26 27 |
# File 'lib/mister_bin/command.rb', line 25 def execute(argv = []) new.execute argv end |
.find_target_command(instance, args) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/mister_bin/command.rb', line 80 def find_target_command(instance, args) target_commands.each do |target| method_name = :"#{target}_command" return method_name if instance.respond_to?(method_name) && args[target.to_s] end :run end |
.help(text) ⇒ Object
35 36 37 |
# File 'lib/mister_bin/command.rb', line 35 def help(text) .help = text end |
.meta ⇒ Object
68 69 70 |
# File 'lib/mister_bin/command.rb', line 68 def @meta ||= CommandMeta.new end |
.option(flags, text) ⇒ Object
47 48 49 |
# File 'lib/mister_bin/command.rb', line 47 def option(flags, text) . << [flags, text] end |
.param(param, text) ⇒ Object
56 57 58 |
# File 'lib/mister_bin/command.rb', line 56 def param(param, text) .params << [param, text] end |
.summary(text) ⇒ Object
DSL
31 32 33 |
# File 'lib/mister_bin/command.rb', line 31 def summary(text) .summary = text end |
.target_commands ⇒ Object
76 77 78 |
# File 'lib/mister_bin/command.rb', line 76 def target_commands @target_commands ||= [] end |
.usage(text) ⇒ Object
43 44 45 |
# File 'lib/mister_bin/command.rb', line 43 def usage(text) .usages << text end |
.version(text) ⇒ Object
39 40 41 |
# File 'lib/mister_bin/command.rb', line 39 def version(text) .version = text end |
Instance Method Details
#execute(argv = []) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/mister_bin/command.rb', line 14 def execute(argv = []) @args = DocoptNG.docopt self.class.docopt, version: self.class..version, argv: argv target = self.class.find_target_command self, args exitcode = send target exitcode.is_a?(Numeric) ? exitcode : 0 rescue DocoptNG::Exit => e puts e. e.exit_code end |