foobara-sh-cli-connector 1.1.2 → 1.1.3
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 +4 -4
- data/README.md +99 -25
- data/src/sh_cli_connector/inputs_parser/option/flag.rb +4 -0
- data/src/sh_cli_connector/inputs_parser/option/off_flag.rb +14 -1
- data/src/sh_cli_connector/inputs_parser/option/on_flag.rb +11 -0
- data/src/sh_cli_connector/inputs_parser/option.rb +13 -1
- data/src/sh_cli_connector/inputs_parser/option_set.rb +4 -4
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2f22467bcc6729b5fe45c66f49dee0083f2cf4a3533b37b9bf1dc564a343f095
|
|
4
|
+
data.tar.gz: 9d4ecbbe05569aa8dd40602dbd1a5d160042e6261637b64a040c3df07634fabb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 135ef5e0da0f1014d053f770b207e9c3232814dd2c5a2de17dee23596ac1be9cd0cda36fa3919d6f540c399e45fac3bd4c247a0ed0cb4b14d89e8ee076f56148
|
|
7
|
+
data.tar.gz: 9dea6d6ede72f50e6477ef1ced1dbdbe48ee42d75935ff1eb48659bc2c73bd67635f249cf97ff0a72198d2b60904b7f56ef46e35de5dfdea7247e18038bfadb3
|
data/README.md
CHANGED
|
@@ -1,48 +1,122 @@
|
|
|
1
1
|
# Foobara::ShCliConnector
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
A command connector for Foobara that exposes commands via a shell command-line interface (CLI). This connector parses command-line arguments and routes them to Foobara commands, making it easy to build CLI tools from your Foobara commands.
|
|
4
|
+
|
|
5
|
+
<!-- TOC -->
|
|
6
|
+
* [Foobara::ShCliConnector](#foobarashcliconnector)
|
|
7
|
+
* [Installation](#installation)
|
|
8
|
+
* [Usage](#usage)
|
|
9
|
+
* [Connecting multiple commands in a CLI script](#connecting-multiple-commands-in-a-cli-script)
|
|
10
|
+
* [Single Command Mode](#single-command-mode)
|
|
11
|
+
* [Development](#development)
|
|
12
|
+
* [Contributing](#contributing)
|
|
13
|
+
* [Reporting bugs or requesting features](#reporting-bugs-or-requesting-features)
|
|
14
|
+
* [Contributing code](#contributing-code)
|
|
15
|
+
* [License](#license)
|
|
16
|
+
<!-- TOC -->
|
|
8
17
|
|
|
9
18
|
## Installation
|
|
10
19
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
20
|
+
Typical stuff: add `gem "foobara-sh-cli-connector"` to your Gemfile or .gemspec file. Or even just
|
|
21
|
+
`gem install foobara-sh-cli-connector` if that's your jam.
|
|
14
22
|
|
|
15
|
-
|
|
23
|
+
## Usage
|
|
16
24
|
|
|
17
|
-
|
|
25
|
+
### Connecting multiple commands in a CLI script
|
|
18
26
|
|
|
19
|
-
|
|
27
|
+
```ruby
|
|
28
|
+
require "foobara/sh_cli_connector"
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
class Greet < Foobara::Command
|
|
31
|
+
inputs do
|
|
32
|
+
who :string, default: "World"
|
|
33
|
+
end
|
|
34
|
+
result :string
|
|
22
35
|
|
|
23
|
-
|
|
36
|
+
def execute
|
|
37
|
+
build_greeting
|
|
24
38
|
|
|
25
|
-
|
|
39
|
+
greeting
|
|
40
|
+
end
|
|
26
41
|
|
|
27
|
-
|
|
42
|
+
attr_accessor :greeting
|
|
43
|
+
|
|
44
|
+
def build_greeting = self.greeting = "Hello, #{who}!"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
connector = Foobara::CommandConnectors::ShCliConnector.new
|
|
48
|
+
connector.connect(Greet)
|
|
49
|
+
connector.run
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Then run your CLI script:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
$ ./cli_demo.rb
|
|
56
|
+
Usage: cli_demo.rb [GLOBAL_OPTIONS] [ACTION] [COMMAND_OR_TYPE] [COMMAND_INPUTS]
|
|
57
|
+
|
|
58
|
+
Available actions:
|
|
59
|
+
|
|
60
|
+
run, help, describe, manifest
|
|
61
|
+
|
|
62
|
+
Default action: run
|
|
63
|
+
|
|
64
|
+
Available commands:
|
|
65
|
+
|
|
66
|
+
Greet
|
|
67
|
+
$ ./cli_demo.rb help Greet
|
|
68
|
+
Usage: cli_demo.rb [GLOBAL_OPTIONS] Greet [COMMAND_INPUTS]
|
|
28
69
|
|
|
70
|
+
Command inputs:
|
|
71
|
+
|
|
72
|
+
-w, --who WHO Default: World
|
|
73
|
+
|
|
74
|
+
$ ./cli_demo.rb Greet
|
|
75
|
+
Hello, World!
|
|
76
|
+
$ ./cli_demo.rb Greet -w Fumiko
|
|
77
|
+
Hello, Fumiko!
|
|
29
78
|
```
|
|
30
|
-
|
|
31
|
-
|
|
79
|
+
|
|
80
|
+
### Single Command Mode
|
|
81
|
+
|
|
82
|
+
If you want to make a CLI script that only exposes one command, you can use single command mode:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
connector = Foobara::CommandConnectors::ShCliConnector.new(single_command_mode: Greet)
|
|
86
|
+
connector.run
|
|
32
87
|
```
|
|
33
88
|
|
|
34
|
-
|
|
35
|
-
|
|
89
|
+
Now you can run this without specifying the command name:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
$ ./greet-cli --help
|
|
93
|
+
Usage: greet-cli [INPUTS]
|
|
36
94
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
95
|
+
Inputs:
|
|
96
|
+
|
|
97
|
+
-w, --who WHO Default: World
|
|
98
|
+
$ ./greet-cli --who Barbara
|
|
99
|
+
Hello, Barbara!
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Development
|
|
40
103
|
|
|
41
104
|
## Contributing
|
|
42
105
|
|
|
43
|
-
|
|
106
|
+
I would love help with this and other Foobara gems! Feel free to hit me up at miles@foobara.org if you
|
|
107
|
+
think helping out would be fun or interesting! I have tasks for all experience levels and am often free
|
|
108
|
+
to pair on Foobara stuff.
|
|
109
|
+
|
|
110
|
+
### Reporting bugs or requesting features
|
|
111
|
+
|
|
112
|
+
Bug reports and feature requests can be made as github issues at https://github.com/foobara/sh-cli-connector
|
|
113
|
+
|
|
114
|
+
### Contributing code
|
|
115
|
+
|
|
116
|
+
You should be able to fork the repo, clone it locally, run `bundle` and then `rake` to run
|
|
117
|
+
the test suite and linter. Make your changes and push them up and open a PR! If you need any help please reach out and we're happy to help!
|
|
44
118
|
|
|
45
119
|
## License
|
|
46
120
|
|
|
47
|
-
|
|
48
|
-
See [LICENSE.txt](LICENSE
|
|
121
|
+
foobara-sh-cli-connector is licensed under your choice of the Apache-2.0 license or the MIT license.
|
|
122
|
+
See [LICENSE.txt](LICENSE.txt) for more info about licensing.
|
|
@@ -11,8 +11,21 @@ module Foobara
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def _long_option_name(prefixed_name)
|
|
14
|
-
"
|
|
14
|
+
"skip-#{Util.kebab_case(prefixed_name)}"
|
|
15
15
|
end
|
|
16
|
+
|
|
17
|
+
# rubocop:disable Naming/PredicateMethod
|
|
18
|
+
def cast_value(value)
|
|
19
|
+
unless value == true
|
|
20
|
+
# Ruby's Optparser handles --no- but not --skip- prefixes. So we are implementing it ourselves.
|
|
21
|
+
# :nocov:
|
|
22
|
+
raise "This shouldn't happen. Please debug this!"
|
|
23
|
+
# :nocov:
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
false
|
|
27
|
+
end
|
|
28
|
+
# rubocop:enable Naming/PredicateMethod
|
|
16
29
|
end
|
|
17
30
|
end
|
|
18
31
|
end
|
|
@@ -4,6 +4,17 @@ module Foobara
|
|
|
4
4
|
class InputsParser
|
|
5
5
|
class Option
|
|
6
6
|
class OnFlag < Flag
|
|
7
|
+
# rubocop:disable Naming/PredicateMethod
|
|
8
|
+
def cast_value(value)
|
|
9
|
+
unless value == true
|
|
10
|
+
# :nocov:
|
|
11
|
+
raise "This shouldn't happen. Please debug this!"
|
|
12
|
+
# :nocov:
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
true
|
|
16
|
+
end
|
|
17
|
+
# rubocop:enable Naming/PredicateMethod
|
|
7
18
|
end
|
|
8
19
|
end
|
|
9
20
|
end
|
|
@@ -57,6 +57,14 @@ module Foobara
|
|
|
57
57
|
# args << attributes_type.declaration_data[:one_of] if attributes_type.declaration_data.key?(:one_of)
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
def cast_value(value)
|
|
61
|
+
if array?
|
|
62
|
+
[value]
|
|
63
|
+
else
|
|
64
|
+
value
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
60
68
|
def _non_colliding_path(full_paths)
|
|
61
69
|
1.upto(full_path.length - 1) do |size|
|
|
62
70
|
candidate_path = _truncated_path(full_path, size)
|
|
@@ -150,6 +158,10 @@ module Foobara
|
|
|
150
158
|
!default.nil?
|
|
151
159
|
end
|
|
152
160
|
|
|
161
|
+
def show_default?
|
|
162
|
+
true
|
|
163
|
+
end
|
|
164
|
+
|
|
153
165
|
def primitive?
|
|
154
166
|
attribute_type.primitive?
|
|
155
167
|
end
|
|
@@ -174,7 +186,7 @@ module Foobara
|
|
|
174
186
|
end
|
|
175
187
|
end
|
|
176
188
|
|
|
177
|
-
if has_default?
|
|
189
|
+
if has_default? && show_default?
|
|
178
190
|
displayable_str = to_bash_str(default)
|
|
179
191
|
desc << "Default: #{displayable_str}"
|
|
180
192
|
end
|
|
@@ -36,11 +36,11 @@ module Foobara
|
|
|
36
36
|
h = h[key] ||= {}
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
+
value = option.cast_value(value)
|
|
40
|
+
h[option.attribute_name] = value
|
|
41
|
+
|
|
39
42
|
result_source.current_array = if option.array?
|
|
40
|
-
|
|
41
|
-
else
|
|
42
|
-
h[option.attribute_name] = value
|
|
43
|
-
nil
|
|
43
|
+
value
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
end
|