CommandLineUtils icon indicating copy to clipboard operation
CommandLineUtils copied to clipboard

How to execute several subcommands/commands with options?

Open holbizmetrics opened this issue 5 years ago • 2 comments

I want to know how I can achieve the following:

Calling several subcommands with options. Code goes below.

First, how the commands should be working, e.g.:

connect --polarionuri <URI> --username <USERNAME> --password <PASSWORD>

this works. Because the correct OnExecute is called.

But now I want to be able to use the connect with the 3 parameters from above for example to execute a test plan. Therefore it is needed that Connect->OnExecute will be executed first. Otherwise the executetestplanid->OnExecute would fail, because there is still no connection.

And I only manage to get either OnExecute to run and not both.

So we'd get the following syntax:

connect --polarionuri <URI> --username <USERNAME> --password <PASSWORD> executetestplanid --testplanid <TESTPLANID>

connect --polarionuri <URI> --username <USERNAME> --password <PASSWORD> retest --assemblyname <ASSEMBLYNAME>

or also with two subcommands:

connect --polarionuri <URI> --username <USERNAME> --password <PASSWORD> executetestplanid --testplanid <TESTPLANID> createtestreport <TESTREPORTFILENAME>

and so on.

This means basically the connect->onexecute method has to be called first, and then the executetestplanid->onexecute should be called.

And this for all commands that are specified. How can I achieve this?

I got the following commands:

    [Command(Name = "TestRunner", Description = "Codan ARGUS Automated Test System"),
        Subcommand(typeof(Connect)),
        Subcommand(typeof(ATSVersion))
    ]
    internal class Program(string[] args)
    { 
        [...]
        returnValue = CommandLineApplication.Execute<Program>(args);
        [...]
    }

 [Command("connect"),
        Subcommand(
        typeof(SelfTest),
        typeof(ReTest),
        typeof(ShowTestPlans),
        typeof(ExecuteTest),
        typeof(ExecuteTestPlan)
    ),
    HelpOption
    ]
    public class Connect
    {
        [Required(ErrorMessage = "You must specify the polarionuri")]
        [Option(Description = "Polarion URI", LongName = "polarionuri", ShortName = "pu")]
        public static string PolarionURI { get; }

        [Required(ErrorMessage = "You must specify the username")]
        [Option(Description = "User Name", ShortName = "u", LongName = "username")]
        public static string PolarionUser { get; }

        [Required(ErrorMessage = "You must specify the password")]
        [Option(Description = "Password", LongName = "password", ShortName = "pw")]
        public static string PolarionPassword { get; }
   }

holbizmetrics avatar Mar 19 '20 15:03 holbizmetrics

I have two suggestions:

  1. You could leverage the "Parent" convention to get a pointer to the parent subcommand, and then invoke methods on that parent class (including the OnExecute method or something else.) Here is a sample: https://github.com/natemcmaster/CommandLineUtils/blob/7281ee9fef08286a3a0a4f72d04b1d234747b316/docs/samples/subcommands/inheritance/Program.cs#L71-L80
  2. It sounds like the execution logic is coupled to your command line parsing structure. You might consider decoupling these into separate classes so you're not bound to the behavior of CommandLineApplication.

natemcmaster avatar Mar 27 '20 15:03 natemcmaster

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

stale[bot] avatar Jul 21 '21 02:07 stale[bot]

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

github-actions[bot] avatar Nov 14 '22 02:11 github-actions[bot]

Closing due to inactivity. If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.

github-actions[bot] avatar Nov 29 '22 02:11 github-actions[bot]