Skip to content

Commit 7f60825

Browse files
Refactor CLI handling --version and --help
This change is necessary in order to allow using them as arguments of a command which is intended for `shards info`.
1 parent 38b3d21 commit 7f60825

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/cli.cr

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require "option_parser"
22
require "./commands/*"
33

44
module Shards
5-
def self.display_help_and_exit(opts)
5+
def self.display_help(opts)
66
puts <<-HELP
77
shards [<options>...] [<command>]
88
@@ -17,29 +17,28 @@ module Shards
1717
prune - Remove unused dependencies from `lib` folder.
1818
update [<shards>] - Update dependencies and `shard.lock`.
1919
version [<path>] - Print the current version of the shard.
20+
--version - Print the `shards` version.
21+
-h, --help - Print usage synopsis.
2022
2123
Options:
2224
HELP
2325
puts opts
24-
exit
2526
end
2627

2728
def self.run
2829
OptionParser.parse(ARGV) do |opts|
2930
path = Dir.current
3031

3132
opts.on("--no-color", "Disable colored output.") { self.colors = false }
32-
opts.on("--version", "Print the `shards` version.") { puts self.version_string; exit }
3333
opts.on("--production", "Run in release mode. No development dependencies and strict sync between shard.yml and shard.lock.") { self.production = true }
3434
opts.on("--local", "Don't update remote repositories, use the local cache only.") { self.local = true }
3535
opts.on("-v", "--verbose", "Increase the log verbosity, printing all debug statements.") { self.logger.level = Logger::Severity::DEBUG }
3636
opts.on("-q", "--quiet", "Decrease the log verbosity, printing only warnings and errors.") { self.logger.level = Logger::Severity::WARN }
37-
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }
3837

3938
opts.unknown_args do |args, options|
40-
case args[0]? || DEFAULT_COMMAND
39+
case args.shift? || DEFAULT_COMMAND
4140
when "build"
42-
build(path, args[1..-1])
41+
build(path, args)
4342
when "check"
4443
Commands::Check.run(path)
4544
when "init"
@@ -51,7 +50,7 @@ module Shards
5150
when "lock"
5251
Commands::Lock.run(
5352
path,
54-
args[1..-1].reject(&.starts_with?("--")),
53+
args.reject(&.starts_with?("--")),
5554
print: args.includes?("--print"),
5655
update: args.includes?("--update")
5756
)
@@ -62,12 +61,17 @@ module Shards
6261
when "update"
6362
Commands::Update.run(
6463
path,
65-
args[1..-1].reject(&.starts_with?("--"))
64+
args.reject(&.starts_with?("--"))
6665
)
6766
when "version"
6867
Commands::Version.run(args[1]? || path)
68+
when "--version"
69+
puts self.version_string
70+
when "-h", "--help"
71+
display_help(opts)
6972
else
70-
display_help_and_exit(opts)
73+
display_help(opts)
74+
exit 1
7175
end
7276

7377
exit

0 commit comments

Comments
 (0)