Skip to content

Commit cb6c7c2

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 2c98f78 commit cb6c7c2

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/cli.cr

Lines changed: 14 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,29 @@ 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 }
35+
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }
3536
opts.on("-v", "--verbose", "Increase the log verbosity, printing all debug statements.") { self.set_debug_log_level }
3637
opts.on("-q", "--quiet", "Decrease the log verbosity, printing only warnings and errors.") { self.set_warning_log_level }
37-
opts.on("-h", "--help", "Print usage synopsis.") { self.display_help_and_exit(opts) }
3838

3939
opts.unknown_args do |args, options|
40-
case args[0]? || DEFAULT_COMMAND
40+
case args.shift? || DEFAULT_COMMAND
4141
when "build"
42-
build(path, args[1..-1])
42+
build(path, args)
4343
when "check"
4444
Commands::Check.run(path)
4545
when "init"
@@ -51,7 +51,7 @@ module Shards
5151
when "lock"
5252
Commands::Lock.run(
5353
path,
54-
args[1..-1].reject(&.starts_with?("--")),
54+
args.reject(&.starts_with?("--")),
5555
print: args.includes?("--print"),
5656
update: args.includes?("--update")
5757
)
@@ -62,12 +62,17 @@ module Shards
6262
when "update"
6363
Commands::Update.run(
6464
path,
65-
args[1..-1].reject(&.starts_with?("--"))
65+
args.reject(&.starts_with?("--"))
6666
)
6767
when "version"
6868
Commands::Version.run(args[1]? || path)
69+
when "--version"
70+
puts self.version_string
71+
when "-h", "--help"
72+
display_help(opts)
6973
else
70-
display_help_and_exit(opts)
74+
display_help(opts)
75+
exit 1
7176
end
7277

7378
exit

0 commit comments

Comments
 (0)