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

Support git-style "--" argument behaviour #77

Closed
wants to merge 1 commit into from

Conversation

tg73
Copy link

@tg73 tg73 commented Apr 10, 2013

Hi Giacomo,

I needed to use commandline in a plugin-like scenario, but where I only wanted arguments to be interpreted at the start of the command line, up to the first non-option argument. In my case, we have a command line utility which roughly speaking represents a flexibly-structured pipeline of files and some command tokens (# in the example). There may be some global options, and each command token might have its own options too, which could quite possibly have the same names as the global options. For example:

myapp -v5 file1 # -v3 file2

So I need to:

  • Process any global options which are at the start of the command line
  • Pass all following arguments to a token parser
  • The token parser will also check for options after the command token (-v3)

My options class:

class GlobalOptions 
{
    [Option( 'v' )]
    public int Verbosity { get; set; }

    [ValueList( typeof( List<string> ) )]
    public IList<string> RemainingArgs { get; set; }
}

By default, commandline will consider -v5 and -v3 as global options. By setting ParserSettings.DoNotInterpretAfterFirstNonOption to true, the parser stops trying to interpret options when it sees file1, and puts file1 and all subsequent arguments into RemainingArgs (including the -v3).

If the filename "file1" actually begins with a dash, it could be a problem, for example:

myapp -v5 -filename_starting_with_dash # -v3 file2

Many git commands have a workaround for this scenario: the double dash argument. The equivalent behaviour can be enabled by setting ParserSettings.EnableDashDash to true, and using:

myapp -v5 -- -filename_starting_with_dash # -v3 file2

I hope you'll see the usefulness of these changes and consider pulling them.

Many thanks,

Tom Glastonbury

…haves as it does for many git commands.

Added support for implicit "--" behaviour via ParserSettings.DoNotInterpretAfterFirstNonOption.
@nemec
Copy link
Collaborator

nemec commented Apr 10, 2013

Thanks for the pull request! I reported this back in #31 but never got around to implementing it.

@tg73
Copy link
Author

tg73 commented Apr 10, 2013

You're welcome! To be honest I just started using commandline today as I
much prefer its API to NDesk.Options, and didn't even look at the roadmap
or outstanding issues. The code changes are quite minor, but perhaps treat
with caution until Giacomo or another dev familiar with the project has
given it the thumbs up.

On 10 April 2013 16:56, Dan Nemec [email protected] wrote:

Thanks for the pull request! I reported this back in #31https://github.com/gsscoder/commandline/issues/31but never got around to implementing it.


Reply to this email directly or view it on GitHubhttps://github.com//pull/77#issuecomment-16183435
.

@gsscoder
Copy link
Owner

@tg73, your contribution is very appreciated (and will be integrated in the new 2.0).

And very happy you choose this library.

As stated here: #67. For the moment I can't accept PR, because the library kernel is under heavy refactoring.

In few time the current master will be freezed in a "stable" branch and the new work will be published in the master.

For being update with this kind of news, I suggest follow me on twitter (@gsscoder).

Regards, Giacomo

@gsscoder
Copy link
Owner

@tg73,
thanks a lot for PR, unfortunately it arrived when library was under heavy refactoring (or better rewrite).

New version supports now this feature.

I've kept the same syntax ParserSettings.EnableDashDash.

Regards,
Giacomo

@gsscoder gsscoder closed this Apr 28, 2013
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

Successfully merging this pull request may close these issues.

3 participants