nub 0.0.67 → 0.0.69

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nub/commander.rb +14 -6
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3df7ecdb78ebe134cdc50b183cd9c70ffd0ac21e9ebfed9a6b0b315a93ce2089
4
- data.tar.gz: 5066c0edf1c3bae95d7d38c07ba215de19123bce35b0b833e1202953e4afbe1b
3
+ metadata.gz: d84be2ede441cb3107962c1c895223d490172d7c6fba52c8c0d3b2a12229694e
4
+ data.tar.gz: 7efb787247ca60748e5584806e7300922daa2cd9e646bfad300061174711c9f4
5
5
  SHA512:
6
- metadata.gz: 37715e0e876c66e1cb081f05a111dbb814e00571017fa970664703c38a2e734a4fadf7a1f87bc27b3c7aa5f9522f12085121b6e5abd292770c71df1d0ccc8c53
7
- data.tar.gz: f4376f093fc3d1b7ab3e9b35119346a859331e40a66567ac0f32aa72048bcfb91cc7178c47decd338314ac6025b8ce965658aab92929222e4d287a1c54268f98
6
+ metadata.gz: cf4a3b69649a686a27998c4fd249243b7a4394d4d574cf55814560ac71e06a7cae6b96d5897182d90a9ee9ef9be5b791dc22e00e982092970658259051e4c574
7
+ data.tar.gz: 154b85284a4aeb66f9ebaec6c90ed4287882be46be1de41eeacc85bcb1a91be6430c182d1175fd3d29f669aa15127dc9ae4f28ab8c5ee5fc1bddba8156275408
data/lib/nub/commander.rb CHANGED
@@ -103,15 +103,18 @@ class Command
103
103
  attr_reader(:desc)
104
104
  attr_accessor(:nodes)
105
105
  attr_accessor(:help)
106
+ attr_accessor(:examples)
106
107
 
107
108
  # Create a new command
108
109
  # @param name [String] command name used on command line
109
110
  # @param desc [String] the command's description
110
111
  # @param nodes [String] the command's description
111
- def initialize(name, desc, nodes:[])
112
+ # @param examples [String] the command's examples
113
+ def initialize(name, desc, nodes:[], examples:nil)
112
114
  @name = name
113
115
  @desc = desc
114
116
  @nodes = nodes
117
+ @examples = examples
115
118
  @help = ""
116
119
  end
117
120
 
@@ -184,7 +187,8 @@ class Commander
184
187
  # @param cmd [String] name of the command
185
188
  # @param desc [String] description of the command
186
189
  # @param nodes [List] list of command nodes (i.e. options or commands)
187
- def add(cmd, desc, nodes:[])
190
+ # @param examples [String] the command's examples
191
+ def add(cmd, desc, nodes:[], examples:nil)
188
192
  Log.die("'#{@k.global}' is a reserved command name") if cmd == @k.global
189
193
  Log.die("'#{cmd}' already exists") if @config.any?{|x| x.name == cmd}
190
194
  Log.die("'help' is a reserved option name") if nodes.any?{|x| x.class == Option && !x.key.nil? && x.key.include?('help')}
@@ -200,7 +204,7 @@ class Commander
200
204
  }
201
205
  nodes.select{|x| x.class != Option}.each{|x| validate_subcmd.(x)}
202
206
 
203
- @config << add_cmd(cmd, desc, nodes)
207
+ @config << add_cmd(cmd, desc, nodes, examples:examples)
204
208
  end
205
209
 
206
210
  # Add global options (any option coming before all commands)
@@ -600,14 +604,18 @@ class Commander
600
604
  # @param nodes [Array] list of command nodes (i.e. options or commands)
601
605
  # @param hierarchy [Array] list of commands
602
606
  # @return [Command] new command
603
- def add_cmd(name, desc, nodes, hierarchy:[])
607
+ def add_cmd(name, desc, nodes, examples:nil, hierarchy:[])
604
608
  hierarchy << name
605
- cmd = Command.new(name, desc)
609
+ cmd = Command.new(name, desc, examples:examples)
606
610
  subcmds = nodes.select{|x| x.class == Command}.sort{|x,y| x.name <=> y.name}
607
611
 
608
612
  # Build help for command
609
613
  #---------------------------------------------------------------------------
610
614
  cmd.help = "#{desc}\n"
615
+ if !cmd.examples.nil? && !cmd.examples.empty?
616
+ newline = cmd.examples.strip_color[-1] != "\n" ? "\n" : ""
617
+ cmd.help += "Examples:\n#{cmd.examples.colorize(:green)}#{newline}"
618
+ end
611
619
  app = @app || @app_default
612
620
  cmd_prompt = subcmds.any? ? "[commands] " : ""
613
621
  cmd.help += "\nUsage: ./#{app} #{hierarchy * ' '} #{cmd_prompt}[options]\n" if name != @k.global
@@ -644,7 +652,7 @@ class Commander
644
652
  cmd.help += "\nsee './#{app} #{name} COMMAND --help' for specific command help\n" if subcmds.any?
645
653
 
646
654
  # Configure help for each sub command
647
- subcmds.each{|x| cmd.nodes << add_cmd(x.name, x.desc, x.nodes, hierarchy:hierarchy)}
655
+ subcmds.each{|x| cmd.nodes << add_cmd(x.name, x.desc, x.nodes, examples:x.examples, hierarchy:hierarchy)}
648
656
 
649
657
  # Add options after any sub-commands
650
658
  cmd.nodes += sorted_options
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.67
4
+ version: 0.0.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Crummett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-15 00:00:00.000000000 Z
11
+ date: 2018-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -120,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  requirements: []
122
122
  rubyforge_project:
123
- rubygems_version: 2.7.6
123
+ rubygems_version: 2.7.7
124
124
  signing_key:
125
125
  specification_version: 4
126
126
  summary: Collection of useful utilities