1

I was wondering if there is an elegant solution to building command lines to launch against an exe, when you potentially have hundreds of parameters.

I guess the most obvious way would be to use if/else statements and build a giant string, but it doesn't feel particularly elegant. Has anyone already solved this problem before?

To Clarify

Say I was writing a program that launched the program notepad.exe, which itself was configurable by accepting command line parameters.

My programs sole purpose is to build the commandline and launch the application.

So I would have a form which had every option that I could set within Notepad.

Radiobutton for WordWrap yes/no. Combobox for Font Inputbox for FontSize.

When I click the "Launch" button I would run "notepad.exe -wordwrap yes -font tahoma -size 8".

So my question is as follows:

  • How would you handle the form logic for building the commandline, would you have a class full of properties for each parameter? And have a Build() method which assembled them all?
  • How would you pass the parameters to the executable?

I'm looking for an elegant solution, not something like:

if (chkWordWrap == true)
 commandline.add("-wordwrap true")

I hope that clarifies what I'm asking :)

6
  • So you just want to know the best practice to handling command line arguments? Commented Nov 15, 2011 at 19:15
  • Are you trying to build permutations of CL args for testing purposes? Commented Nov 15, 2011 at 19:16
  • No, not handling command lines, I want to use my application to build a commandline and launch an executable that accepts those commands :). Commented Nov 15, 2011 at 19:19
  • 1
    An example would be very helpful to understand what you want. Just concenating parameters to a string does not need any if/else, so what the heck are you talking of? Commented Nov 15, 2011 at 19:19
  • Well the form would comprise of options for launching the program, lets using notepad as an example, say it took commandline parameters to set it's options, you would build a command line such as: "notepad.exe -wordwrap true -font Tahoma -size 8" Now saw in my application I had a dropdown for setting the font, and an inputbox for the fontsize, and a checkbox for setting word wrap, and when I click "Run notepad" it builds that command line and executes it: What would be an elegant way of capturing the form data and building it into a command line. Just to clarify, it's not for notepad :) Commented Nov 15, 2011 at 19:25

4 Answers 4

2

If you have an executable that accepts hundredths of parameters on the commandline, it's unlikely that that particular executable expected you to type those parameters literally on the commandline. Most of these programs can work with input files that contain their commandlines.

Elegantly: let your program write such input file and run it against the program the normal way using Process, something along these lines:

Process proc = new Process();
proc.StartInfo.FileName = "externalprogram.exe";
proc.StartInfo.Arguments = "input file here.txt";
proc.Start();
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, what if it's a program that expects its commandline to be submitted programmatically, therefore doesn't accept an input file?
@toleero: can you define "programmatically"? Because even if you cannot use an input file, you can use proc.StartInfo.Arguments to contain zillions of commandline parameters if you wish and whether you start Notepad or want to run your-incredibly-many-parameters-program.exe shouldn't really matter.
@toleero: so, what I'm saying, this is the .NET way to build a commandline for an external program programmatically.
Brilliant, thanks! How would you handle the form logic? If you had loads of drop downs, radiobuttons, comboboxes, would you have a big class with Properties backing each command? I guess you could use get/set to set its parameter and have a "Name" field for each command which would be the actual command?
@toleero, it really depends on how you're used to program and what patterns you're comfortable with. If it's a one-off application I'd probably create extension methods like GetParamValue and GetParamName for each type of Control and call that when you loop through all controls on your form. You could use a property of the controls to contain the name of the argument.
1

You can try to have list/map of objects that have parameter name and possible values (or way to compute value based on UI element) and then itereate through all items and output parameter name + value.

class Parameter
{
 public Parameter(Control control)...
 public string name;
 public bool neededBasedOnControl()...
 public string valueByControl()...
}

List<Parameter> allParameters = new List<Parameter>();
allParameters.Add(new Parameter(myControl42);

...
StringBuilder args;
foreach(var p in allParameter)
{
  if (p.neededBasedOnControl())
  {
    args.Format(p.name, p.valueByControl);
  }
}

Comments

0

I'm a little uncertain what you're asking for, but it sounds like you're looking for an elegant way to parse command line arguments. I would suggest taking a look at this argument parser on CodeProject. Using the parser you can get arguments like this:

static void Main(string[] args)
{
    Arguments cmdArgs = new Arguments(args);

    Console.WriteLine(cmdArgs["arg_name"]);
}

Comments

0

To build command line string, you might try the 'Heleonix.Execution' library, which provides the ArgsBuilder class: https://github.com/Heleonix/Heleonix.Execution/blob/master/README.md You can build a command line string as below:

var args = ArgsBuilder.By("--", "=")
            .AddPath("app", "http://www.google.com")
            .AddArgument("window-size", "300,300", isSized)
            .AddKey("new-window", isNewWindow);

// Depending on conditions, command line arguments can be like:

// "--app="http://www.google.com" --window-size=300,300 --new-window"
// "--app="http://www.google.com" --window-size=300,300"
// "--app="http://www.google.com" --new-window"
// "--app="http://www.google.com""

You can add separate keys, separate values, key/value arguments, paths (which are wrapped with "..." by default) and lists of all of the above with conditional expressions defining whether they should be added or not (instead of writing if statements). Any of your command line arguments can be built using the ArgsBuilder itself, so you can have nesting, because the ArgsBuilder instance is casted implicitly to the System.String, so no need to call *.ToString()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.