simple-cli 0.3.11 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/README.md +51 -0
- data/VERSION +1 -1
- data/doc/examples/ex1/ex1 +2 -0
- data/doc/examples/ex1/ex1.rb +41 -0
- data/lib/simple/cli/deprecations.rb +19 -0
- data/lib/simple/cli/logger/colored_logger.rb +18 -3
- data/lib/simple/cli/runner.rb +2 -4
- data/lib/simple/cli.rb +7 -0
- data/simple-cli.gemspec +1 -1
- metadata +10 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcf0aaedcec401d2a1d7b28d2463aa6b331626c3d75b22b9d8e2c9251c489370
|
4
|
+
data.tar.gz: 7c45a6520ea7d7f8812e4f66a6acd2c57c107866527b578c279a06fb58ce0ed7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65eb1d3681b02768d7f0b24805dd8f7c730cdb9cafefb9154b4990607b6d0760d671995fbedb27f36c411b7202a373036e6b09df55bbe6ff293757c1a98a1edf
|
7
|
+
data.tar.gz: d67979bd430b4b2d7854e789029ef14dcc42138195d9b1520904a1ddf92bf2bfc1c21feec2bbbca3517ff6122f1a7cab98ea871a4e36d3d61d93ec3478ebf849
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.2
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# simple-cli
|
2
|
+
|
3
|
+
Building Ruby CLIs with easy.
|
4
|
+
|
5
|
+
## Philosophy
|
6
|
+
|
7
|
+
Building a command line could be much easier than what ruby provides with its standard library. In general, between the invocation of a tool from the command line and the actual running of whatever code the user intends to run really should disappear.
|
8
|
+
|
9
|
+
Ideally a developer would only have to build the implementation of whatever logic should be provided by the tool; everything else - parsing arguments, calling the tool's implementation, printing help - should happen automatically.
|
10
|
+
|
11
|
+
This is roughly what sinmple-sql provides.
|
12
|
+
|
13
|
+
## On versions
|
14
|
+
|
15
|
+
`simple-cli` is still on a 0.x.x version, and, generally, a 0.x.y version is not compatible with a 0.x+1.y version.
|
16
|
+
|
17
|
+
These are the follow
|
18
|
+
|
19
|
+
- The 0.2.x versions are tracked in the [`stable`](https://github.com/radiospiel/simple-cli/tree/stable) branch
|
20
|
+
- The 0.3.x versions are tracked in the [`master`](https://github.com/radiospiel/simple-cli/tree/stable) branch
|
21
|
+
|
22
|
+
## Basic features
|
23
|
+
|
24
|
+
- build command line features in a number of modules;
|
25
|
+
- public methods in these modules provide a subcommand for the CLI;
|
26
|
+
- `"_"` in the method name are being mapped to `":"` in the CLI;
|
27
|
+
- CLI provides a help subcommand. help texts are derived from the methods over a subcommand implementation:
|
28
|
+
- The first line determines the "short help" message, to be included in the `$CMD help` command list;
|
29
|
+
- The remaining lines determines the full help message, to be included in the `$CMD help subcommand` message.
|
30
|
+
|
31
|
+
## Example
|
32
|
+
|
33
|
+
An example can be found in [./doc/examples/ex1](./doc/examples/ex1)
|
34
|
+
|
35
|
+
## Use logging
|
36
|
+
|
37
|
+
`Simple::CLI` provides a logger instance. This is configured to write to STDERR, and to use colors for different log levels. By default the logger is configured to run on INFO log levels. With the `--quiet` flag the logger is running at WARN log level; the `--verbose` command line flag runs the logger on DEBUG log level, and also includes the source position of calling log.
|
38
|
+
|
39
|
+
## Updating to version 0.3
|
40
|
+
|
41
|
+
While the 0.2 version is still perfectly functional, its last version was released on Jul 5th, 2019. Development on the 0.3 versions started with some refactoring: the logic that inspects a subcommand invocation and determines its argument names and default types has been moved to a `simple-services' gem.
|
42
|
+
|
43
|
+
To upgrade from simple-cli version 0.2 to version 0.3 all you typically have to do is to replace the
|
44
|
+
|
45
|
+
YourApp::CLI.run!(*ARGV)
|
46
|
+
|
47
|
+
invocation with
|
48
|
+
|
49
|
+
Simple::CLI.run!(YourApp::CLI)
|
50
|
+
|
51
|
+
This [commit](https://github.com/radiospiel/simple-cli/commit/3e75bd6fb913a2b458269c91597c42cabac226b4) provides an example of doing that.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'bundler/inline'
|
3
|
+
|
4
|
+
gemfile do
|
5
|
+
source 'https://rubygems.org'
|
6
|
+
gem "simple-cli", '~> 0.3', path: "../../.."
|
7
|
+
end
|
8
|
+
|
9
|
+
module Ex1; end
|
10
|
+
module Ex1::CLI
|
11
|
+
include Simple::CLI
|
12
|
+
|
13
|
+
# Command without arguments
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# ./ex1 hello:world
|
18
|
+
#
|
19
|
+
def hello_world
|
20
|
+
puts "Hello from #{__FILE__}"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Command with arguments
|
24
|
+
#
|
25
|
+
# This implements a command with arguments
|
26
|
+
#
|
27
|
+
# Examples:
|
28
|
+
#
|
29
|
+
# ./ex1 hello --name=user "what's up"
|
30
|
+
#
|
31
|
+
def hello(message, name: nil)
|
32
|
+
if name
|
33
|
+
puts "Hello #{name}: #{message}!"
|
34
|
+
else
|
35
|
+
puts "Hello, #{message}!"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
$0 = "ex1"
|
41
|
+
Simple::CLI.run!(Ex1::CLI)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ::Simple::CLI::Deprecations
|
2
|
+
SELF = self
|
3
|
+
|
4
|
+
def logger
|
5
|
+
SELF.deprecated! "Simple::CLI#logger", alternative: "Simple::CLI.logger"
|
6
|
+
end
|
7
|
+
|
8
|
+
def run!(*_)
|
9
|
+
SELF.deprecated! "Simple::CLI#run!", alternative: "Simple::CLI.run!(Your::CLI)"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.deprecated!(what, alternative: nil)
|
13
|
+
Simple::CLI.logger.error "'#{what}' was removed."
|
14
|
+
if alternative
|
15
|
+
Simple::CLI.logger.info "You probably want to use '#{alternative}' instead."
|
16
|
+
end
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
end
|
@@ -152,10 +152,25 @@ module Simple::CLI::Logger::ColoredLogger
|
|
152
152
|
Pathname.new(absolute_path).relative_path_from(@pwd_pathname).to_s
|
153
153
|
end
|
154
154
|
|
155
|
-
# The heuristic used to determine the caller is not perfect
|
156
|
-
#
|
155
|
+
# [TODO] The heuristic used to determine the caller is not perfect.
|
156
|
+
# Maybe we'll find a better solution; but for now this has to do.
|
157
157
|
def source_from_caller
|
158
|
-
source = caller.find
|
158
|
+
source = caller.find do |loc|
|
159
|
+
# skip this gem
|
160
|
+
next false if loc =~ /\/lib\/simple\/cli\//
|
161
|
+
|
162
|
+
# skip forwardable from Ruby stdlib
|
163
|
+
next false if loc =~ /\/forwardable.rb\:/
|
164
|
+
|
165
|
+
# skip simple-sql
|
166
|
+
next false if loc =~ /\/lib\/simple\/sql\b/
|
167
|
+
|
168
|
+
# skip lib/postjob/queue/postgres/checked_sql.rb
|
169
|
+
next false if loc =~ %r{lib/postjob/queue/postgres/checked_sql.rb}
|
170
|
+
|
171
|
+
true
|
172
|
+
end
|
173
|
+
|
159
174
|
source ||= caller[2]
|
160
175
|
source = source[(wd.length + 1)..-1] if source.start_with?(wd)
|
161
176
|
source
|
data/lib/simple/cli/runner.rb
CHANGED
@@ -10,10 +10,8 @@ module Simple::CLI
|
|
10
10
|
_ = verbose
|
11
11
|
|
12
12
|
action_name = H.command_to_action(command)
|
13
|
-
|
14
|
-
|
15
|
-
::Simple::Service.invoke(service, action_name, args: args, flags: flags)
|
16
|
-
end
|
13
|
+
flags = extract_flags!(args)
|
14
|
+
::Simple::Service.invoke(service, action_name, args: args, flags: flags)
|
17
15
|
end
|
18
16
|
|
19
17
|
private
|
data/lib/simple/cli.rb
CHANGED
@@ -13,6 +13,7 @@ require_relative "cli/adapter"
|
|
13
13
|
require_relative "cli/logger"
|
14
14
|
require_relative "cli/on_exception"
|
15
15
|
require_relative "cli/helpers"
|
16
|
+
require_relative "cli/deprecations"
|
16
17
|
|
17
18
|
require "simple/service"
|
18
19
|
|
@@ -29,6 +30,11 @@ module Simple::CLI
|
|
29
30
|
def self.included(base)
|
30
31
|
base.include(::Simple::Service)
|
31
32
|
base.include(::Simple::CLI::Helpers)
|
33
|
+
|
34
|
+
# When going from 0.2 to 0.3 we removed some helpers that originally had been
|
35
|
+
# mixed into the target CLI module. The ::Simple::CLI::Deprecations module
|
36
|
+
# makes sure to print deprecation warnings.
|
37
|
+
base.extend(::Simple::CLI::Deprecations)
|
32
38
|
end
|
33
39
|
|
34
40
|
# Runs the service with the current command line arguments.
|
@@ -81,6 +87,7 @@ module Simple::CLI
|
|
81
87
|
# Run service.
|
82
88
|
Runner.run! service, options.command, *args, verbose: options.verbose?
|
83
89
|
rescue ::Simple::Service::ArgumentError
|
90
|
+
STDERR.puts "#{$!}\n\n"
|
84
91
|
Helper.help_on_command! service, options.command, verbose: false
|
85
92
|
rescue StandardError => e
|
86
93
|
on_exception(e)
|
data/simple-cli.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2022-06-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: simple-service
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 0.1
|
20
|
+
version: 0.2.1
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 0.1
|
27
|
+
version: 0.2.1
|
28
28
|
description: Simple CLI builder
|
29
29
|
email: eno@radiospiel.org
|
30
30
|
executables: []
|
@@ -33,14 +33,19 @@ extra_rdoc_files: []
|
|
33
33
|
files:
|
34
34
|
- ".gitignore"
|
35
35
|
- ".rubocop.yml"
|
36
|
+
- ".ruby-version"
|
36
37
|
- Gemfile
|
38
|
+
- README.md
|
37
39
|
- Rakefile
|
38
40
|
- VERSION
|
39
41
|
- bin/rake
|
42
|
+
- doc/examples/ex1/ex1
|
43
|
+
- doc/examples/ex1/ex1.rb
|
40
44
|
- lib/simple-cli.rb
|
41
45
|
- lib/simple/cli.rb
|
42
46
|
- lib/simple/cli/adapter.rb
|
43
47
|
- lib/simple/cli/default_options.rb
|
48
|
+
- lib/simple/cli/deprecations.rb
|
44
49
|
- lib/simple/cli/helper.rb
|
45
50
|
- lib/simple/cli/helper/help.rb
|
46
51
|
- lib/simple/cli/helper/help_on_command.rb
|
@@ -77,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
82
|
- !ruby/object:Gem::Version
|
78
83
|
version: '0'
|
79
84
|
requirements: []
|
80
|
-
rubygems_version: 3.
|
85
|
+
rubygems_version: 3.1.4
|
81
86
|
signing_key:
|
82
87
|
specification_version: 4
|
83
88
|
summary: Simple CLI builder for ruby
|