diff --git a/spec/integration/help_spec.cr b/spec/integration/help_spec.cr new file mode 100644 index 00000000..9bb5db30 --- /dev/null +++ b/spec/integration/help_spec.cr @@ -0,0 +1,29 @@ +require "./spec_helper" + +describe "--help" do + it "prints help and doesn't invoke the command" do + metadata = { + version: "1.0.0", + dependencies: { + mock: {git: git_path("mock")}, + }, + } + + [ + "shards --help", + "shards --local --help", + "shards update --help", + ].each do |command| + with_shard(metadata) do + output = run command + + # it printed the help message + output.should contain("Commands:") + output.should contain("General options:") + + # it didn't run the command (or default command) + output.should_not contain("Resolving dependencies") + end + end + end +end diff --git a/src/cli.cr b/src/cli.cr index beb8ad24..0ace4160 100644 --- a/src/cli.cr +++ b/src/cli.cr @@ -2,6 +2,20 @@ require "option_parser" require "./commands/*" module Shards + BUILTIN_COMMANDS = %w[ + build + run + check + init + install + list + lock + outdated + prune + update + version + ] + def self.display_help_and_exit(opts) puts <<-HELP shards [...] [] @@ -58,50 +72,56 @@ module Shards opts.on("-h", "--help", "Print usage synopsis.") { display_help = true } opts.unknown_args do |args, options| - case args[0]? || DEFAULT_COMMAND - when "build" - targets, build_options = parse_args(args[1..-1]) - check_and_install_dependencies(path) - - Commands::Build.run(path, targets, build_options) - when "run" - targets, run_options = parse_args(args[1..-1]) - check_and_install_dependencies(path) - - Commands::Run.run(path, targets, run_options, options) - when "check" - Commands::Check.run(path) - when "init" - Commands::Init.run(path) - when "install" - Commands::Install.run( - path - ) - when "list" - Commands::List.run(path, tree: args.includes?("--tree")) - when "lock" - Commands::Lock.run( - path, - args[1..-1].reject(&.starts_with?("--")), - print: args.includes?("--print"), - update: args.includes?("--update") - ) - when "outdated" - Commands::Outdated.run( - path, - prereleases: args.includes?("--pre") - ) - when "prune" - Commands::Prune.run(path) - when "update" - Commands::Update.run( - path, - args[1..-1].reject(&.starts_with?("--")) - ) - when "version" - Commands::Version.run(args[1]? || path) + command = args[0]? || DEFAULT_COMMAND + + if BUILTIN_COMMANDS.includes?(command) + if display_help + display_help_and_exit(opts) + end + + case command + when "build" + targets, build_options = parse_args(args[1..-1]) + check_and_install_dependencies(path) + Commands::Build.run(path, targets, build_options) + when "run" + targets, run_options = parse_args(args[1..-1]) + check_and_install_dependencies(path) + Commands::Run.run(path, targets, run_options, options) + when "check" + Commands::Check.run(path) + when "init" + Commands::Init.run(path) + when "install" + Commands::Install.run(path) + when "list" + Commands::List.run(path, tree: args.includes?("--tree")) + when "lock" + Commands::Lock.run( + path, + args[1..-1].reject(&.starts_with?("--")), + print: args.includes?("--print"), + update: args.includes?("--update") + ) + when "outdated" + Commands::Outdated.run( + path, + prereleases: args.includes?("--pre") + ) + when "prune" + Commands::Prune.run(path) + when "update" + Commands::Update.run( + path, + args[1..-1].reject(&.starts_with?("--")) + ) + when "version" + Commands::Version.run(args[1]? || path) + else + raise "BUG: unknown command #{command}" + end else - program_name = "shards-#{args[0]}" + program_name = "shards-#{command}" if program_path = Process.find_executable(program_name) run_shards_subcommand(program_path, cli_options) else @@ -109,10 +129,6 @@ module Shards end end - if display_help - display_help_and_exit(opts) - end - exit end end