pedrozath-mercenary 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +174 -0
- data/.travis.yml +17 -0
- data/Gemfile +6 -0
- data/History.markdown +115 -0
- data/LICENSE.txt +22 -0
- data/README.md +249 -0
- data/Rakefile +3 -0
- data/examples/help_dialogue.rb +45 -0
- data/examples/logging.rb +35 -0
- data/examples/trace.rb +20 -0
- data/lib/mercenary/command.rb +283 -0
- data/lib/mercenary/option.rb +88 -0
- data/lib/mercenary/presenter.rb +97 -0
- data/lib/mercenary/program.rb +65 -0
- data/lib/mercenary/version.rb +5 -0
- data/lib/mercenary.rb +23 -0
- data/mercenary.gemspec +26 -0
- data/script/bootstrap +7 -0
- data/script/cibuild +7 -0
- data/script/console +3 -0
- data/script/examples +18 -0
- data/script/fmt +4 -0
- data/spec/command_spec.rb +98 -0
- data/spec/option_spec.rb +84 -0
- data/spec/presenter_spec.rb +41 -0
- data/spec/program_spec.rb +19 -0
- data/spec/spec_helper.rb +16 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: abfc352cd1288064314d2d38a2a8fb159167944af3ed6321b1ac582e11381f9f
|
4
|
+
data.tar.gz: 17d7e7f5ed239b55dc7ed7f58364aa109e0f9747514465fb9d920101c86bbd69
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 574871d15def1f7ac1a920b0f19bf6b6d20c97f3da6387f080ea01fef30f505d842f273ad2c287eade19d1633e962ff6b8f07ef311718ce244b824ce2aaf9efb
|
7
|
+
data.tar.gz: d6d092702829952e1a0989b617b1edc37122d319cbb151cfc85977325cd6e11bb9a16d7ff1f5d510a91853c69834cd447428d446db2aa3bda1217657564c2411
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
---
|
2
|
+
AllCops:
|
3
|
+
TargetRubyVersion: 2.0
|
4
|
+
Exclude:
|
5
|
+
- vendor/**/*
|
6
|
+
Layout/AlignArray:
|
7
|
+
Enabled: false
|
8
|
+
Layout/AlignHash:
|
9
|
+
EnforcedHashRocketStyle: table
|
10
|
+
Layout/AlignParameters:
|
11
|
+
Enabled: false
|
12
|
+
Layout/EmptyLinesAroundAccessModifier:
|
13
|
+
Enabled: false
|
14
|
+
Layout/EmptyLinesAroundModuleBody:
|
15
|
+
Enabled: false
|
16
|
+
Layout/EndOfLine:
|
17
|
+
EnforcedStyle: native
|
18
|
+
Layout/ExtraSpacing:
|
19
|
+
AllowForAlignment: true
|
20
|
+
Layout/FirstParameterIndentation:
|
21
|
+
EnforcedStyle: consistent
|
22
|
+
Layout/IndentationWidth:
|
23
|
+
Severity: error
|
24
|
+
Layout/IndentArray:
|
25
|
+
EnforcedStyle: consistent
|
26
|
+
Layout/IndentHash:
|
27
|
+
EnforcedStyle: consistent
|
28
|
+
Layout/IndentHeredoc:
|
29
|
+
Enabled: false
|
30
|
+
Exclude:
|
31
|
+
- spec/**/*
|
32
|
+
Layout/MultilineMethodCallIndentation:
|
33
|
+
EnforcedStyle: indented
|
34
|
+
Layout/MultilineOperationIndentation:
|
35
|
+
EnforcedStyle: indented
|
36
|
+
Layout/SpaceAroundOperators:
|
37
|
+
Enabled: true
|
38
|
+
Layout/SpaceInsideBrackets:
|
39
|
+
Enabled: false
|
40
|
+
Lint/AssignmentInCondition:
|
41
|
+
Exclude:
|
42
|
+
- lib/mercenary/command.rb
|
43
|
+
- lib/mercenary/option.rb
|
44
|
+
- lib/mercenary/presenter.rb
|
45
|
+
Lint/DuplicateMethods:
|
46
|
+
Exclude:
|
47
|
+
- lib/mercenary/command.rb
|
48
|
+
Lint/EndAlignment:
|
49
|
+
Severity: error
|
50
|
+
Lint/HandleExceptions:
|
51
|
+
Exclude:
|
52
|
+
- lib/mercenary/option.rb
|
53
|
+
Lint/RescueWithoutErrorClass:
|
54
|
+
Enabled: false
|
55
|
+
Exclude:
|
56
|
+
- lib/mercenary/program.rb
|
57
|
+
Lint/UselessAssignment:
|
58
|
+
Exclude:
|
59
|
+
- lib/mercenary/presenter.rb
|
60
|
+
Lint/UnreachableCode:
|
61
|
+
Severity: error
|
62
|
+
Lint/UselessAccessModifier:
|
63
|
+
Enabled: false
|
64
|
+
Lint/Void:
|
65
|
+
Enabled: false
|
66
|
+
Metrics/AbcSize:
|
67
|
+
Max: 21
|
68
|
+
Exclude:
|
69
|
+
- lib/mercenary/program.rb
|
70
|
+
Metrics/BlockLength:
|
71
|
+
Exclude:
|
72
|
+
- examples/**/*
|
73
|
+
- spec/**/*
|
74
|
+
Metrics/ClassLength:
|
75
|
+
Max: 300
|
76
|
+
Metrics/CyclomaticComplexity:
|
77
|
+
Max: 9
|
78
|
+
Metrics/LineLength:
|
79
|
+
Exclude:
|
80
|
+
- lib/mercenary/command.rb
|
81
|
+
- lib/mercenary/presenter.rb
|
82
|
+
- lib/mercenary/program.rb
|
83
|
+
- examples/**/*
|
84
|
+
- spec/**/*
|
85
|
+
- mercenary.gemspec
|
86
|
+
Max: 90
|
87
|
+
Severity: warning
|
88
|
+
Metrics/MethodLength:
|
89
|
+
Exclude:
|
90
|
+
- lib/mercenary/program.rb
|
91
|
+
CountComments: false
|
92
|
+
Max: 20
|
93
|
+
Severity: error
|
94
|
+
Metrics/ModuleLength:
|
95
|
+
Max: 240
|
96
|
+
Metrics/ParameterLists:
|
97
|
+
Max: 4
|
98
|
+
Metrics/PerceivedComplexity:
|
99
|
+
Max: 8
|
100
|
+
Naming/FileName:
|
101
|
+
Enabled: false
|
102
|
+
Naming/HeredocDelimiterNaming:
|
103
|
+
Enabled: false
|
104
|
+
Naming/PredicateName:
|
105
|
+
Exclude:
|
106
|
+
- lib/mercenary/command.rb
|
107
|
+
Style/Alias:
|
108
|
+
Enabled: false
|
109
|
+
Style/AndOr:
|
110
|
+
Severity: error
|
111
|
+
Style/Attr:
|
112
|
+
Enabled: false
|
113
|
+
Style/BracesAroundHashParameters:
|
114
|
+
Enabled: false
|
115
|
+
Style/ClassAndModuleChildren:
|
116
|
+
Enabled: false
|
117
|
+
Style/FrozenStringLiteralComment:
|
118
|
+
Exclude:
|
119
|
+
- lib/mercenary/command.rb
|
120
|
+
Enabled: true
|
121
|
+
EnforcedStyle: always
|
122
|
+
Style/Documentation:
|
123
|
+
Enabled: false
|
124
|
+
Style/DoubleNegation:
|
125
|
+
Enabled: false
|
126
|
+
Style/Encoding:
|
127
|
+
EnforcedStyle: when_needed
|
128
|
+
Style/GuardClause:
|
129
|
+
Enabled: false
|
130
|
+
Style/HashSyntax:
|
131
|
+
EnforcedStyle: hash_rockets
|
132
|
+
Severity: error
|
133
|
+
Style/IfUnlessModifier:
|
134
|
+
Enabled: false
|
135
|
+
Style/InverseMethods:
|
136
|
+
Enabled: false
|
137
|
+
Style/MethodMissing:
|
138
|
+
Exclude:
|
139
|
+
- lib/mercenary/presenter.rb
|
140
|
+
Style/ModuleFunction:
|
141
|
+
Enabled: false
|
142
|
+
Style/MultilineTernaryOperator:
|
143
|
+
Severity: error
|
144
|
+
Style/PercentLiteralDelimiters:
|
145
|
+
PreferredDelimiters:
|
146
|
+
"%q": "{}"
|
147
|
+
"%Q": "{}"
|
148
|
+
"%r": "!!"
|
149
|
+
"%s": "()"
|
150
|
+
"%w": "()"
|
151
|
+
"%W": "()"
|
152
|
+
"%x": "()"
|
153
|
+
Style/RedundantReturn:
|
154
|
+
Enabled: false
|
155
|
+
Style/RedundantSelf:
|
156
|
+
Enabled: false
|
157
|
+
Style/RegexpLiteral:
|
158
|
+
EnforcedStyle: percent_r
|
159
|
+
Style/RescueModifier:
|
160
|
+
Enabled: false
|
161
|
+
Style/SignalException:
|
162
|
+
EnforcedStyle: only_raise
|
163
|
+
Style/SingleLineMethods:
|
164
|
+
Enabled: false
|
165
|
+
Style/StringLiterals:
|
166
|
+
EnforcedStyle: double_quotes
|
167
|
+
Style/StringLiteralsInInterpolation:
|
168
|
+
EnforcedStyle: double_quotes
|
169
|
+
Style/SymbolArray:
|
170
|
+
Enabled: false
|
171
|
+
Style/TrailingCommaInLiteral:
|
172
|
+
EnforcedStyleForMultiline: consistent_comma
|
173
|
+
Style/UnneededCapitalW:
|
174
|
+
Enabled: false
|
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
6
|
+
- 2.3
|
7
|
+
- 2.4
|
8
|
+
sudo: false
|
9
|
+
cache: bundler
|
10
|
+
before_script: bundle update
|
11
|
+
script: script/cibuild
|
12
|
+
notifications:
|
13
|
+
email:
|
14
|
+
recipients:
|
15
|
+
- mercenary@jekyllrb.com
|
16
|
+
on_success: change
|
17
|
+
on_failure: change
|
data/Gemfile
ADDED
data/History.markdown
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
## HEAD
|
2
|
+
|
3
|
+
### Development Fixes
|
4
|
+
|
5
|
+
* Adopt Jekyll's rubocop config for consistency (#51)
|
6
|
+
|
7
|
+
## 0.3.6 / 2016-04-07
|
8
|
+
|
9
|
+
### Bug Fixes
|
10
|
+
|
11
|
+
* Presenter: Options should include those from parent command (#42)
|
12
|
+
|
13
|
+
## 0.3.5 / 2014-11-12
|
14
|
+
|
15
|
+
### Bug Fixes
|
16
|
+
|
17
|
+
* Capture `OptionsParser::InvalidOption` and show a nice error message (#38)
|
18
|
+
* Absolute paths for requires and autoloads (#39)
|
19
|
+
|
20
|
+
### Development Fixes
|
21
|
+
|
22
|
+
* Bump to RSpec 3 (#40)
|
23
|
+
|
24
|
+
## 0.3.4 / 2014-07-11
|
25
|
+
|
26
|
+
### Bug Fixes
|
27
|
+
|
28
|
+
* Use option object as key in the command's `@map` hash (#35)
|
29
|
+
|
30
|
+
## 0.3.3 / 2014-05-07
|
31
|
+
|
32
|
+
### Bug Fixes
|
33
|
+
|
34
|
+
* The `--version` flag should not exit with code 1, but instead code 0. (#33)
|
35
|
+
|
36
|
+
## 0.3.2 / 2014-03-18
|
37
|
+
|
38
|
+
### Bug Fixes
|
39
|
+
|
40
|
+
* Remove duplicate commands from help output; show aliases w/command names (#29)
|
41
|
+
|
42
|
+
## 0.3.1 / 2014-02-21
|
43
|
+
|
44
|
+
### Minor Enhancements
|
45
|
+
|
46
|
+
* Add `-t/--trace` to list of options in help message (#19)
|
47
|
+
|
48
|
+
### Bug Fixes
|
49
|
+
|
50
|
+
* `Mercenary::Option` now accepts return values in the form of Class constants (#22)
|
51
|
+
|
52
|
+
## 0.3.0 / 2014-02-20
|
53
|
+
|
54
|
+
### Major Enhancements
|
55
|
+
|
56
|
+
* Officially drop 1.8.7 support (#14)
|
57
|
+
* Allow Commands to set their own versions (#17)
|
58
|
+
* Show subcommands, options and usage in help and attach to all commands (#18)
|
59
|
+
* Add `-t, --trace` to allow full exception backtrace to print, otherwise print just the error message (#19)
|
60
|
+
|
61
|
+
### Minor Enhancements
|
62
|
+
|
63
|
+
* Logging state is maintained throughout process (#12)
|
64
|
+
* Tidy up Command#logger output (#21)
|
65
|
+
|
66
|
+
### Development Fixes
|
67
|
+
|
68
|
+
* Added specs for `Program` (#13)
|
69
|
+
|
70
|
+
## 0.2.1 / 2013-12-25
|
71
|
+
|
72
|
+
### Bug Fixes
|
73
|
+
|
74
|
+
* Added missing comma to fix '-v' and '--version' options (#9)
|
75
|
+
|
76
|
+
## 0.2.0 / 2013-11-30
|
77
|
+
|
78
|
+
### Major Enhancements
|
79
|
+
|
80
|
+
* Add `Command#default_command` to specify a default command if none is given by the user at runtime (#7)
|
81
|
+
|
82
|
+
### Minor Enhancements
|
83
|
+
|
84
|
+
* Add `Command#execute` to execute the actions of a command (#6)
|
85
|
+
|
86
|
+
### Development Fixes
|
87
|
+
|
88
|
+
* Add standard GitHub bootstrap and cibuild scripts to `script/` (#2)
|
89
|
+
|
90
|
+
## 0.1.0 / 2013-11-08
|
91
|
+
|
92
|
+
### Major Enhancements
|
93
|
+
|
94
|
+
* It works!
|
95
|
+
|
96
|
+
### Minor Enhancements
|
97
|
+
|
98
|
+
* Add a logger to `Command`
|
99
|
+
* Add `--version` switch to all programs
|
100
|
+
|
101
|
+
### Bug Fixes
|
102
|
+
|
103
|
+
* Fix `Command#syntax` and `Command#description`'s handing of setting vs getting
|
104
|
+
* Fix load path problem in `lib/mercenary.rb`
|
105
|
+
|
106
|
+
### Development Fixes
|
107
|
+
|
108
|
+
* Add TomDoc to everything
|
109
|
+
* Add a couple starter specs
|
110
|
+
* Add TravisCI badge
|
111
|
+
* Add Travis configuration
|
112
|
+
|
113
|
+
## 0.0.1 / 2013-11-06
|
114
|
+
|
115
|
+
* Birthday!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013-present Parker Moore and the mercenary contributors
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,249 @@
|
|
1
|
+
# Mercenary
|
2
|
+
|
3
|
+
Lightweight and flexible library for writing command-line apps in Ruby.
|
4
|
+
|
5
|
+
[](https://travis-ci.org/jekyll/mercenary)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'mercenary'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install mercenary
|
20
|
+
|
21
|
+
**Note: Mercenary may not work with Ruby < 1.9.3.**
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Creating programs and commands with Mercenary is easy:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Mercenary.program(:jekyll) do |p|
|
29
|
+
p.version Jekyll::VERSION
|
30
|
+
p.description 'Jekyll is a blog-aware, static site generator in Ruby'
|
31
|
+
p.syntax "jekyll <subcommand> [options]"
|
32
|
+
|
33
|
+
p.command(:new) do |c|
|
34
|
+
c.syntax "new PATH" # do not include the program name or super commands
|
35
|
+
c.description "Creates a new Jekyll site scaffold in PATH"
|
36
|
+
c.option 'blank', '--blank', 'Initialize the new site without any content.'
|
37
|
+
|
38
|
+
c.action do |args, options|
|
39
|
+
Jekyll::Commands::New.process(args, blank: options['blank'])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
p.command(:build) do |c|
|
44
|
+
c.syntax "build [options]"
|
45
|
+
c.description "Builds your Jekyll site"
|
46
|
+
|
47
|
+
c.option 'safe', '--safe', 'Run in safe mode'
|
48
|
+
c.option 'source', '--source DIR', 'From where to collect the source files'
|
49
|
+
c.option 'destination', '--dest DIR', 'To where the compiled files should be written'
|
50
|
+
|
51
|
+
c.action do |_, options|
|
52
|
+
Jekyll::Commands::Build.process(options)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Bring in command bundled in external gem
|
57
|
+
begin
|
58
|
+
require "jekyll-import"
|
59
|
+
JekyllImport.init_with_program(p)
|
60
|
+
rescue LoadError
|
61
|
+
end
|
62
|
+
|
63
|
+
p.default_command(:build)
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
All commands have the following default options:
|
68
|
+
|
69
|
+
- `-h/--help` - show a help message
|
70
|
+
- `-v/--version` - show the program version
|
71
|
+
- `-t/--trace` - show the full backtrace when an error occurs
|
72
|
+
|
73
|
+
## API
|
74
|
+
|
75
|
+
### `Mercenary`
|
76
|
+
|
77
|
+
#### `.program`
|
78
|
+
|
79
|
+
Creates and executes a program. Accepts two arguments:
|
80
|
+
|
81
|
+
- `name` - program name as a Symbol
|
82
|
+
- `block` - the specification for the program, passed the program instance as an
|
83
|
+
argument.
|
84
|
+
|
85
|
+
Example is above, under the heading [Usage](#usage).
|
86
|
+
|
87
|
+
### `Program`
|
88
|
+
|
89
|
+
`Program` is a subclass of `Command`, so it has all of the methods documented
|
90
|
+
below as well as those for `Command`.
|
91
|
+
|
92
|
+
#### `#config`
|
93
|
+
|
94
|
+
Fetches the program configuration hash.
|
95
|
+
|
96
|
+
### `Command`
|
97
|
+
|
98
|
+
#### `#new`
|
99
|
+
|
100
|
+
Create a new command. Accepts one argument:
|
101
|
+
|
102
|
+
- `name` - the name of your command, as a symbol
|
103
|
+
|
104
|
+
#### `#version`
|
105
|
+
|
106
|
+
Sets or gets the version of the command. Accepts an optional argument:
|
107
|
+
|
108
|
+
- `version` - (optional) the version to set for the command. If present, this
|
109
|
+
becomes the new version for the command and persists.
|
110
|
+
|
111
|
+
#### `#syntax`
|
112
|
+
|
113
|
+
Sets or gets the syntax of the command. Built on parent syntaxes if a parent
|
114
|
+
exists. Accepts one optional argument:
|
115
|
+
|
116
|
+
- `syntax` - (optional) the syntax to set for the command. Will inherit from the
|
117
|
+
parent commands or program. Usually in the form of
|
118
|
+
`"command_name <SUBCOMMAND> [OPTIONS]"`
|
119
|
+
|
120
|
+
When a parent command exists, say `supercommand`, with syntax set as
|
121
|
+
`supercommand <SUBCOMMAND> [OPTIONS]`, the syntax of the command in question
|
122
|
+
will be `supercommand command_name <SUBCOMMAND> [OPTIONS]` with both
|
123
|
+
`<SUBCOMMAND>` and `[OPTIONS]` stripped out. Any text between `<` and `>` or
|
124
|
+
between `[` and `]` will be stripped from parent command syntaxes. The purpose
|
125
|
+
of this chaining is to reduce redundancy.
|
126
|
+
|
127
|
+
#### `#description`
|
128
|
+
|
129
|
+
Sets or gets the description of the command. Accepts one optional argument:
|
130
|
+
|
131
|
+
- `desc` - (optional) the description to set for the command. If
|
132
|
+
provided, will override any previous description set for the command.
|
133
|
+
|
134
|
+
#### `#default_command`
|
135
|
+
|
136
|
+
Sets or gets the default subcommand of the command to execute in the event no
|
137
|
+
subcommand is passed during execution. Accepts one optional argument:
|
138
|
+
|
139
|
+
- `command_name` - (optional) the `Symbol` name of the subcommand to be
|
140
|
+
executed. Raises an `ArgumentError` if the subcommand doesn't exist.
|
141
|
+
Overwrites previously-set default commands.
|
142
|
+
|
143
|
+
#### `#option`
|
144
|
+
|
145
|
+
Adds a new option to the command. Accepts many arguments:
|
146
|
+
|
147
|
+
- `config_key` - the configuration key that the value of this option maps to.
|
148
|
+
- `*options` - all the options, globbed, to be passed to `OptionParser`, namely the
|
149
|
+
switches and the option description. Usually in the format
|
150
|
+
`"-s", "--switch", "Sets the 'switch' flag"`.
|
151
|
+
|
152
|
+
Valid option calls:
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
cmd.option 'config_key', '-c', 'Sets the "config" flag'
|
156
|
+
cmd.option 'config_key', '--config', 'Sets the "config" flag'
|
157
|
+
cmd.option 'config_key', '-c', '--config', 'Sets the "config" flag.'
|
158
|
+
cmd.option 'config_key', '-c FILE', '--config FILE', 'The config file.'
|
159
|
+
cmd.option 'config_key', '-c FILE1[,FILE2[,FILE3...]]', '--config FILE1[,FILE2[,FILE3...]]', Array, 'The config files.'
|
160
|
+
```
|
161
|
+
|
162
|
+
Notice that you can specify either a short switch, a long switch, or both. If
|
163
|
+
you want to accept an argument, you have to specify it in the switch strings.
|
164
|
+
The class of the argument defaults to `String`, but you can optionally set a
|
165
|
+
different class to create, e.g. `Array`, if you are expecting a particular class
|
166
|
+
in your code from this option's value. The description is also optional, but
|
167
|
+
it's highly recommended to include a description.
|
168
|
+
|
169
|
+
#### `#alias`
|
170
|
+
|
171
|
+
Specifies an alias for this command such that the alias may be used in place of
|
172
|
+
the command during execution. Accepts one argument:
|
173
|
+
|
174
|
+
- `cmd_name` - the alias name for this command as a `Symbol`
|
175
|
+
|
176
|
+
Example:
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
cmd.alias(:my_alias)
|
180
|
+
# Now `cmd` is now also executable via "my_alias"
|
181
|
+
```
|
182
|
+
|
183
|
+
#### `#action`
|
184
|
+
|
185
|
+
Specifies a block to be executed in the event the command is specified at
|
186
|
+
runtime. The block is given two arguments:
|
187
|
+
|
188
|
+
- `args` - the non-switch arguments given from the command-line
|
189
|
+
- `options` - the options hash built via the switches passed
|
190
|
+
|
191
|
+
**Note that actions are additive**, meaning any new call to `#action` will
|
192
|
+
result in another action to be executed at runtime. Actions will be executed in
|
193
|
+
the order they are specified in.
|
194
|
+
|
195
|
+
Example:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
cmd.action do |args, options|
|
199
|
+
# do something!
|
200
|
+
end
|
201
|
+
```
|
202
|
+
|
203
|
+
#### `#logger`
|
204
|
+
|
205
|
+
Access the logger for this command. Useful for outputting information to STDOUT.
|
206
|
+
Accepts one optional argument:
|
207
|
+
|
208
|
+
- `level` - (optional) the severity threshold at which to begin logging. Uses
|
209
|
+
Ruby's built-in
|
210
|
+
[`Logger`](http://www.ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html)
|
211
|
+
levels.
|
212
|
+
|
213
|
+
Log level defaults to `Logger::INFO`.
|
214
|
+
|
215
|
+
Examples:
|
216
|
+
|
217
|
+
```ruby
|
218
|
+
cmd.logger(Logger::DEBUG)
|
219
|
+
cmd.logger.debug "My debug message."
|
220
|
+
cmd.logger.info "My informative message."
|
221
|
+
cmd.logger.warn "ACHTUNG!!"
|
222
|
+
cmd.logger.error "Something terrible has happened."
|
223
|
+
cmd.logger.fatal "I can't continue doing what I'm doing."
|
224
|
+
```
|
225
|
+
|
226
|
+
#### `#command`
|
227
|
+
|
228
|
+
Creates a new subcommand for the current command. Accepts two arguments:
|
229
|
+
|
230
|
+
- `cmd_name` - the command name, as a Symbol
|
231
|
+
- `block` - the specification of the subcommand in a block
|
232
|
+
|
233
|
+
Example:
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
my_command.command(:my_subcommand) do |subcmd|
|
237
|
+
subcmd.description 'My subcommand'
|
238
|
+
subcmd.syntax 'my_subcommand [OPTIONS]'
|
239
|
+
# ...
|
240
|
+
end
|
241
|
+
```
|
242
|
+
|
243
|
+
## Contributing
|
244
|
+
|
245
|
+
1. Fork it
|
246
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
247
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
248
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
249
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.join(__dir__, "..", "lib")
|
5
|
+
|
6
|
+
require "mercenary"
|
7
|
+
|
8
|
+
# This example sets the logging mode of mercenary to
|
9
|
+
# debug. Logging messages from "p.logger.debug" will
|
10
|
+
# be output to STDOUT.
|
11
|
+
|
12
|
+
Mercenary.program(:help_dialogue) do |p|
|
13
|
+
p.version "2.0.1"
|
14
|
+
p.description "An example of the help dialogue in Mercenary"
|
15
|
+
p.syntax "help_dialogue <subcommand>"
|
16
|
+
|
17
|
+
p.command(:some_subcommand) do |c|
|
18
|
+
c.version "1.4.2"
|
19
|
+
c.syntax "some_subcommand <subcommand> [options]"
|
20
|
+
c.description "Some subcommand to do something"
|
21
|
+
c.option "an_option", "-o", "--option", "Some option"
|
22
|
+
c.alias(:blah)
|
23
|
+
|
24
|
+
c.command(:yet_another_sub) do |f|
|
25
|
+
f.syntax "yet_another_sub [options]"
|
26
|
+
f.description "Do amazing things"
|
27
|
+
f.option "blah", "-b", "--blah", "Trigger blah flag"
|
28
|
+
f.option "heh", "-H ARG", "--heh ARG", "Give a heh"
|
29
|
+
|
30
|
+
f.action do |args, options|
|
31
|
+
print "Args: "
|
32
|
+
p args
|
33
|
+
print "Opts: "
|
34
|
+
p options
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
p.command(:another_subcommand) do |c|
|
40
|
+
c.syntax "another_subcommand <subcommand> [options]"
|
41
|
+
c.description "Another subcommand to do something different."
|
42
|
+
c.option "an_option", "-O", "--option", "Some option"
|
43
|
+
c.option "another_options", "--pluginzzz", "Set where the plugins should be found from"
|
44
|
+
end
|
45
|
+
end
|
data/examples/logging.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.join(__dir__, "..", "lib")
|
5
|
+
|
6
|
+
require "mercenary"
|
7
|
+
|
8
|
+
# This example sets the logging mode of mercenary to
|
9
|
+
# debug. Logging messages from "p.logger.debug" will
|
10
|
+
# be output to STDOUT.
|
11
|
+
|
12
|
+
Mercenary.program(:logger_output) do |p|
|
13
|
+
p.version "5.2.6"
|
14
|
+
p.description "An example of turning on logging for Mercenary."
|
15
|
+
p.syntax "logger_output"
|
16
|
+
|
17
|
+
p.logger.info "The default log level is INFO. So this will output."
|
18
|
+
p.logger.debug "Since DEBUG is below INFO, this will not output."
|
19
|
+
|
20
|
+
p.logger(Logger::DEBUG)
|
21
|
+
p.logger.debug "Logger level now set to DEBUG. So everything will output."
|
22
|
+
|
23
|
+
p.logger.debug "Example of DEBUG level message."
|
24
|
+
p.logger.info "Example of INFO level message."
|
25
|
+
p.logger.warn "Example of WARN level message."
|
26
|
+
p.logger.error "Example of ERROR level message."
|
27
|
+
p.logger.fatal "Example of FATAL level message."
|
28
|
+
p.logger.unknown "Example of UNKNOWN level message."
|
29
|
+
|
30
|
+
p.action do |_args, _options|
|
31
|
+
p.logger(Logger::INFO)
|
32
|
+
p.logger.debug "Logger level back to INFO. This line will not output."
|
33
|
+
p.logger.info "This INFO message will output."
|
34
|
+
end
|
35
|
+
end
|