mosop/optarg

Feature Request – Silent Unknowns

jaitaiwan opened this issue · 2 comments

I have a use case where I want to create an OptArg model which checks for "sub commands" and then I find another OptArg model based on the subcommand.

At the moment, because any extra options besides the sub commands cause an UnknownOption exception to be thrown it prevents me from being able to elegantly handle things this way.

Is there room for a feature which allows for Silent unknown options? If so what would be the best way to implement so I can create a pull request?

mosop commented

Hi. Actually, I'm currently struggle to design such APIs that allow users to handle parser's error on their own ways. That "Silent Unknown" feature is a little big decision for me, so please wait for me to think out.

Instead, you can use the stop option on your super command's definition for now.

class Supercommand < Optarg::Model
  arg "subcommand", stop: true
end

Then you can get a subcommand name and unparsed arguments:

result = Supercommand.parse(%w(subcomamnd --opt arg))
result.subcommand # => "subcommand"
result.unparsed_args # => ["--opt", "arg"]

I implemented subcommand handling with this technique in the cli library, that is my another library for building CLI applications. See the source code if you like.

Thank you.

Oh great! That will work for now thanks.