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

Dealing with HelpNeeded #73

Closed
ioquatix opened this issue Apr 15, 2016 · 2 comments
Closed

Dealing with HelpNeeded #73

ioquatix opened this issue Apr 15, 2016 · 2 comments

Comments

@ioquatix
Copy link

Hi.

I want to implement help for sub-commands, e.g.

mycommand subcommand --help

The best way I can see to do this is to somehow override the exception handling but I was wondering if there was a better way?

@nanobowers
Copy link
Collaborator

@ioquatix maybe something like this hack of the sub-command example?

#!/bin/env ruby
require 'trollop'

SUB_COMMANDS = %w(delete copy)
global_opts = Trollop::options do
  banner "magic file deleting and copying utility"
  opt :dry_run, "Don't actually do anything", :short => "-n"
  stop_on SUB_COMMANDS
end

cmd = ARGV.shift # get the subcommand
cmd_opts = case cmd
  when "delete" # parse delete options
    Trollop::options do
      banner "Different banner for delete command"
      opt :force, "Force deletion"
      opt :help, "Customized other help message for delete"
    end
  when "copy"  # parse copy options
    Trollop::options do
      banner "Yet another banner for copy command"
      opt :double, "Copy twice for safety's sake"
    end
  else
    Trollop::die "unknown subcommand #{cmd.inspect}, should be one of #{SUB_COMMANDS}"
  end

puts "Global options: #{global_opts.inspect}"
puts "Subcommand: #{cmd.inspect}"
puts "Subcommand options: #{cmd_opts.inspect}"
puts "Remaining arguments: #{ARGV.inspect}"

@ioquatix
Copy link
Author

@nanobowers Thanks so much for a useful example.

In the end, my needs were too great for trollop, a library which I really loved. I still think it's an awesome library, but I ended up reinventing the wheel, and the results have been great so far.

https://github.com/ioquatix/samovar

It's still a work in progress.

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

2 participants