-
Notifications
You must be signed in to change notification settings - Fork 71
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
Commands should support arguments #72
Comments
Thanks for reporting this. I should have some time tomorrow to look into it a little more. |
How about something like this: require 'slop'
opts = Slop::Commands.new do
on :install
end
opts.parse %w(install geronimo)
p opts.arguments #=> ["geronimo"] |
Looks good, but still there is no way to tell, which command has been triggered: opts = Slop::Commands.new do
on :install
on :list
end
opts.parse %w(install geronimo)
p opts.arguments #=> ["geronimo"]
# Is this an argument of `install` or maybe `list`? |
I can't see why you would ever care which command was used. I see something like this: if opts[:install]
run_installer(opts.arguments)
end And just pass around |
I don't know, maybe I don't understand something, but this opts = Slop::Commands.new do
on :install
on :list
end
opts.parse %w(install geronimo)
p opts[:install]
#=> #<Slop {:strict=>false, :help=>false, :banner=>nil, :ignore_case=>false, :autocreate=>false, :arguments=>false, :optional_arguments=>false, :multiple_switches=>true, :longest_flag=>0} []>
p opts[:list]
#=> #<Slop {:strict=>false, :help=>false, :banner=>nil, :ignore_case=>false, :autocreate=>false, :arguments=>false, :optional_arguments=>false, :multiple_switches=>true, :longest_flag=>0} []> P.S.: Also, keep in mind the situation when we want to pass multiple arguments (like we can do it with options: |
Yeah you're right. I think temporarily something like this will have to do: opts = Slop::Commands.new do
on :install
on :list
end
opts.parse %w(install geronimo)
if opts.present?(:install)
# do something with opts.arguments
end Which I guess is just short for
That is, I think the command interface probably needs an overhaul but I'd rather plan and spend the time getting it right for Slop v4. |
Closed by #76 |
Currently, there is no way to implement parsing of commands that accept arguments. Let's consider a basic Slop v3 command set:
Let's try to parse some input:
The problem with current code is that it doesn't support arguments for
install
subcommand. Of course, you can save the return value ofCommands#parse
and use it in your program, but let's have a look at the situation when our command set have more than one command:Both calls return the same value. Given a stroppy user of our CLI tool, it's pretty obvious that we can't tell, which of the subcommands has been executed from program's point of view:
install
orlist
?It seems like the only way to go is to use command options:
Quite frankly, this pretty verbose and simply sucks.
Possible solutions
Add
:with_argument
parameter hash toCommands#on
This parameter should be specific for each command.
Treat one of subcommand options as a default option
The text was updated successfully, but these errors were encountered: