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

Getting the "current" verb #160

Closed
ericnewton76 opened this issue Nov 4, 2017 · 1 comment
Closed

Getting the "current" verb #160

ericnewton76 opened this issue Nov 4, 2017 · 1 comment

Comments

@ericnewton76
Copy link
Member

Issue by jeffeld
Wednesday Aug 23, 2017 at 10:56 GMT
Originally opened as gsscoder/commandline#471


In the code below, I'd like to be able to either:

  • Pass the parsed verb to RunTask or
  • Retrieve the verb in RunTask

Is either one or both possible using 2.1.1-beta?

Thanks,
Jeff

`
static int Main(string[] args)
{

        int exitCode = CommandLine.Parser.Default.ParseArguments<AuditorOpinionScanOptions, TestTaskOptions>(args)
            .MapResult(
                (AuditorOpinionScanOptions options) => RunTask<AuditorOpinionScanTask, AuditorOpinionScanOptions>(options),
                (TestTaskOptions options) => RunTask<TestTask, TestTaskOptions>(options),
                errs => 1
            );

        


        return exitCode;

    }


    private static int RunTask<TTask, TOptions>(TOptions options) where TTask: ITask<TOptions>, new()
    {
        TTask task = new TTask()
        {
            Log = CreateLogger(),
            Options = options
        };

        return task.Execute();
    }

`

@ericnewton76
Copy link
Member Author

Comment by jeffeld
Wednesday Aug 23, 2017 at 12:51 GMT


So after a little thinking, it dawned on me the verb is an attribute on the options class. A little Googleing turned up this little gem.

Adding the extention method in Darin Dimitrov's answer (dated April 16 2010) allowed me to add this line to RunTask()...

string verb = typeof(TOptions).GetAttributeValue((VerbAttribute verbAttribute) => verbAttribute.Name);
...and use it where required.

Would be interested to know if there is a better way, but if not please go ahead and close this.

Cheers,
Jeff

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