Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI with default task and a required argument #370

Open
wrightling opened this issue Nov 6, 2013 · 7 comments
Open

CLI with default task and a required argument #370

wrightling opened this issue Nov 6, 2013 · 7 comments

Comments

@wrightling
Copy link

I am creating a very simple CLI for my gem, and would like the user to be able to simply type a single word (name of the CLI), followed by some arguments, i.e.:

$ test 'arg1'

First question: is having my main class inherit from Thor::Group instead of Thor the only way to do this, as follows?

#!/usr/bin/env ruby

require 'thor'

class Test < Thor::Group
  argument :arg, type: :string
  def foo
    puts "bar"
  end
end

Test.start

Given that script, I can run it as follows without trouble:

$ ./test.rb 'some arg'

Second question: How can I make the help stop essentially listing the name of the CLI twice. Here is what "./test.rb --help" shows me:

Usage:
  test.rb test ARG

I would rather it read simply:

Usage:
  test.rb ARG

To whit, this help would be even more awkward once I package into a gem, where the help would read something like:

Usage:
  test test ARG
@christianbundy
Copy link

I'm having the exact same problem, and haven't found a solution. I'd greatly appreciate any tips or tricks to working with this. Thanks!

@arthurnn
Copy link
Member

Hi, I am not sure if I understand the issue in here.
First of all, in order to use your command test you need to place a test file under a bin folder. Remeber that the name of the file has to be test and not test.rb.
For a better example, you can follow this gem that uses thor in a really simple way to create a exec command, https://github.com/arthurnn/cheatly/blob/master/bin/cheatly .

@christianbundy
Copy link

We want to be able to have a command with absolutely no subcommand. For example, check out christianbundy/load.

I'd like to be able to use the command:

load christianbundy/project where christianbundy/project is a subcommand.

Additionally, load help should read

Usage:
  load ARG

@philliplongman
Copy link

I would also like this, and I find it strange that it's not already the behavior of default command to try to feed args that aren't commands into the default command.

@revolter
Copy link

Same problem.

@TeWu
Copy link

TeWu commented Apr 15, 2018

First of all, I hope there will be some nicer way of doing it in future version of Thor.

My quite ugly, but working workaround:

#!/usr/bin/env ruby
require 'thor'

class MyApp < Thor
  default_task :list

  desc "TARGET_DIR", "List all files in directory and subdirectories"
  def list(target_dir)
    puts "Listing files in #{target_dir} now"
  end

  map %w[--version -v] => :version
  desc "--version, -v", "Print the version"
  def version
    puts "#{MyApp} 0.0.1"
  end

end

ARGV.unshift(MyApp.default_task) unless MyApp.all_tasks.has_key?(ARGV[0]) || MyApp.instance_variable_get(:@map).has_key?(ARGV[0])
MyApp.start(ARGV)

With this class I can run it from command line:

$ my_app --help
Commands:
  my_app --version, -v   # Print the version
  my_app TARGET_DIR      # List all files in directory and subdirectories
  my_app help [COMMAND]  # Describe available commands or one specific command

$ my_app -v
MyApp 0.0.1

$ my_app -vv
Listing files in -vv now

$ my_app path/to/some/dir
Listing files in path/to/some/dir now

$ my_app list path/to/some/dir
Listing files in path/to/some/dir now

@mohkale
Copy link

mohkale commented Mar 20, 2020

I can't find the default_task method on the wiki, it should probably be there 😅.

mjgiarlo added a commit to sul-dlss/sdr-deploy that referenced this issue Mar 15, 2022
Why? Partly to get around needing workarounds such as rails/thor#370 (comment) for single-task Thor classes, and partly to make it easier to get useful CLI help. Before this change, the CLI help system worked like as follows: `bin/check_cocina help check_cocina`. Notice that to get help with a command, you would need to supply it twice. AFAIK, there is not a more Thor-like way to handle this... except to do what is done in this commit. Now `bin/sdr help check_cocina` does what you would expect and so does `bin/sdr help`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants