Reputation: 18465
How can I pass a command line option to another class?
I have a program.cs that have arguments
static void Main(string[] args)
{
// One of args element is an output folder path
}
I'm actually using CommandLine Parser
static void Main(string[] args)
{
CommandLine.Parser.Default.ParseArguments<Options>(args)
.MapResult((opts) => RunOptions(opts), // in case parser sucess
errs => HandleParseError(errs)); // in case parser fail
}
With an option folder like this
internal class Options
{
[Option('o', "output", Required = true, HelpText = "Output folder where to save the report.")]
public string OutputFolder { get; set; }
}
You can see I force the user to give me an output folder. for those that don't know how Command Line Parser is working just consider I can save one of the arguments in an OutputFolder
property of an Option
object.
I want to be sure I can access my args or my Option.OutputFolder property from everywhere in my program. I don't want to have to pass my Option or args in all my methods.
What should I do?
In fact I would like to be able to access my arguments a little bit like I can access my variables in my app.config file. Is it possible?
Upvotes: 0
Views: 192
Reputation: 17545
RunOptions
would need to thread the opts
variable to all the methods through parameters, or it can set a static property/field which other things can read. Up to the design of your application and your preferences.
There's nothing built-in to the CommandLineParser library to access the parsed data statically, so you need to maintain the data connection yourself.
Upvotes: 1