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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66e8c8336729e6c840ee38fec2f6b8d96237f078e1974c0fc2a38193307a1d29
4
- data.tar.gz: d2a0652b6e70a782d46dc49dc5843cb30881465471db652d3f9a0f08be98139e
3
+ metadata.gz: 2f22467bcc6729b5fe45c66f49dee0083f2cf4a3533b37b9bf1dc564a343f095
4
+ data.tar.gz: 9d4ecbbe05569aa8dd40602dbd1a5d160042e6261637b64a040c3df07634fabb
5
5
  SHA512:
6
- metadata.gz: 22f34b1310e54975cc3839e27bf3993ca51f49248a2cff8d204ad410348bd1b668de63fedfdc7c443a7b4c36374fdb96e335a8416a88737289a95060c389afef
7
- data.tar.gz: 7bad46487ed10907757345ba223a236724d0a073937d04b2831ecb3d7185d9b449f3fe761ac8b3b75e6b657fe70af81f64e564afdf397146d5888254258c5c55
6
+ metadata.gz: 135ef5e0da0f1014d053f770b207e9c3232814dd2c5a2de17dee23596ac1be9cd0cda36fa3919d6f540c399e45fac3bd4c247a0ed0cb4b14d89e8ee076f56148
7
+ data.tar.gz: 9dea6d6ede72f50e6477ef1ced1dbdbe48ee42d75935ff1eb48659bc2c73bd67635f249cf97ff0a72198d2b60904b7f56ef46e35de5dfdea7247e18038bfadb3
data/README.md CHANGED
@@ -1,48 +1,122 @@
1
1
  # Foobara::ShCliConnector
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library
6
- into a gem. Put your Ruby code in the file `lib/foobara/sh_cli_connector`. To experiment with that code,
7
- run `bin/console` for an interactive prompt.
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it
12
- to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with
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
- Install the gem and add to the application's Gemfile by executing:
23
+ ## Usage
16
24
 
17
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
25
+ ### Connecting multiple commands in a CLI script
18
26
 
19
- If bundler is not being used to manage dependencies, install the gem by executing:
27
+ ```ruby
28
+ require "foobara/sh_cli_connector"
20
29
 
21
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
30
+ class Greet < Foobara::Command
31
+ inputs do
32
+ who :string, default: "World"
33
+ end
34
+ result :string
22
35
 
23
- ## Usage
36
+ def execute
37
+ build_greeting
24
38
 
25
- TODO: Write usage instructions here
39
+ greeting
40
+ end
26
41
 
27
- ## Development
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
- bundle config set local.foobara ../foobara
31
- bundle config set disable_local_branch_check true
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
35
- also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
38
- version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
39
- push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/foobara-sh_cli_connector.
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
- This project is licensed under your choice of the Apache-2.0 license or the MIT license.
48
- See [LICENSE.txt](LICENSE-MIT.txt) for more info about licensing.
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.
@@ -53,6 +53,10 @@ module Foobara
53
53
  def array?
54
54
  false
55
55
  end
56
+
57
+ def show_default?
58
+ false
59
+ end
56
60
  end
57
61
  end
58
62
  end
@@ -11,8 +11,21 @@ module Foobara
11
11
  end
12
12
 
13
13
  def _long_option_name(prefixed_name)
14
- "no-#{Util.kebab_case(prefixed_name)}"
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
- h[option.attribute_name] = [value]
41
- else
42
- h[option.attribute_name] = value
43
- nil
43
+ value
44
44
  end
45
45
  end
46
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-sh-cli-connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi