CommandLineUtils
CommandLineUtils copied to clipboard
How to execute several subcommands/commands with options?
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; }
}
I have two suggestions:
- 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
- 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.
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.
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.
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.