question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get result of parsed command line arguments as an object

See original GitHub issue

Is your feature request related to a problem? Please describe. Yes, I would like to have an “CommandLineArguments” object with immutable fields that I could pass around my application.

Describe the solution you’d like Instead of using int CommandLineApplication.Execute<ProgramSettings>(args); I’d like to have a method ProgramSettings ParseCommandLineArguments<ProgramSettings>(args); so that I could pass “Program” around.

Describe alternatives you’ve considered Currently, I’m doing the following Program.cs contains the entry point of the program and the following code:

public static int Main(string[] args) {
	if (args.Length == 1 && args[0] == "--lazy-dev-switch")
		args = LazyDevArguments();

	return CommandLineApplication.Execute<ProgramSettings>(args);
}

private static string[] LazyDevArguments(){
...
}

public static int Run(ProgramSettings settings) {
	... DoStuffWith(settings);
}

It is ugly, but at least it allows me to pass a well-formed “ProgramSettings” object around.

And in ProgramSettings.cs I have all the Options and whatnot PLUS the method OnExecute():

public int OnExecute() {
	return Program.Run(this);
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
natemcmastercommented, Sep 4, 2019

So, in other words, you want all the goodness of CommandLineApplication.Execute<Settings> without the requirement to implement an OnExecute method on the Settings class? If so, this is something I’ve considered adding but was basically waiting to see if anyone actually wanted it.

2reactions
natemcmastercommented, Sep 4, 2019

Dependency injection.

Yeah, the heavy lifting is done. This is really a matter of designing an API that works well. It’s possible to implement some of this on your own with CommandLineApplication and an empty OnExecute (see example here) but this is more effort than I think should be necessary.

I’ve wanted to expose a lower level API that parsed args into a simpler dictionary-like result first. Then this result is bound to an object or the CommandArgument/Option types. I nearly wrote it into this library last year, but ended up helping the team that wrote System.CommandLine build this approach instead. I’ve been tempted to use it as the “processing engine” under the hood, but System.CommandLine doesn’t appear to be on track for a stable release anytime soon. So, I’m thinking maybe I should do it on my own instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linking command line argument parsing with object ...
I have a class which has around a dozen object variables. Along with each variable, I want to provide a default value, a...
Read more >
argparse — Parser for command-line options, arguments ...
In a script, parse_args() will typically be called with no arguments, and the ArgumentParser will automatically determine the command-line arguments from sys.
Read more >
How to parse command line arguments in node.js
Run the arithmetic.js file by passing the following arguments: Output: Program to perform the arithmetic operation according to the arguments ...
Read more >
How to Parse Command-Line Arguments in Python
To retrieve the values of the command-line arguments, call the parse_args() method on the ArgumentParser object. Next, learn how to perform ...
Read more >
ArgumentResult Class (System.CommandLine.Parsing)
Finds a result for the specific option anywhere in the parse tree, including parent and child symbol results. (Inherited from SymbolResult). GetHashCode().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found