Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does there exist an established standard for testing command line arguments?

I am developing a command line utility that has a LOT of flags. A typical command looks like this:

mycommand --foo=A --bar=B --jar=C --gnar=D --binks=E

In most cases, a 'success' message is printed but I still want to verify against other sources like an external database to ensure actual success.

I'm starting to create integration tests and I am unsure of the best way to do this. My main concerns are:

  1. There are many many flag combinations, how do I know which combinations to test? If you do the math for the 10+ flags that can be used together...
  2. Is it necessary to test permutations of flags?
  3. How to build a framework capable of automating the tests and then verifying results.
  4. How to keep track of a large number of flags and providing an order so it is easy to tell what combinations have been implemented and what has not.

The thought of manually writing out individual cases and verifying results in a unit-test like format is daunting.

Does anyone know of a pattern that can be used to automate this type of test? Perhaps even software that attempts to solve this problem? How did people working on GNU commandline tools test their software?

like image 212
trinth Avatar asked Apr 29 '11 04:04

trinth


People also ask

What is a command line argument?

Command line arguments allow the user to affect the operation of an application. For example, an application might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information--with the command line argument -verbose .

Which is concern with command line argument?

On most UNIX platforms, command-line argument processing is handled using the getopt function. This function parses arguments passed to a program via the command line, with each option and possible argument made available to the programmer via a switch statement.

What is the first argument of command line?

argv[1] is the first command-line argument. The last argument from the command line is argv[argc - 1] , and argv[argc] is always NULL. For information on how to suppress command-line processing, see Customize C++ command-line processing. By convention, argv[0] is the filename of the program.

What is command line arguments in CPP?

They are parameters/arguments supplied to the program when it is invoked. They are used to control program from outside instead of hard coding those values inside the code. argv[argc] is a NULL pointer. argv[0] holds the name of the program.


2 Answers

I think this is very specific to your application.

First, how do you determine the success of the execution of you application? Is it a result code? Is it something printed to the console?

For question 2, it depends how you parse those flags in your application. Most of the time, order of flags isn't important, but there are cases where it is. I hope you don't need to test for permutations of flags, because it would add a lot of cases to test.

In a general case, you should analyse what is the impact of each flag. It is possible that a flag doesn't interfere with the others, and then it just need to be tested once. This is also the case for flags that are meant to be used alone (--help or --version, for example). You also need to analyse what values you should test for each flag. Usually, you want to try each kind of possible valid value, and each kind of possible invalid values.

I think a simple bash script could be written to perform the tests, or any scripting language, like Python. Using nested loops, you could try, for each flag, possibles values, including tests for invalid values and the case where the flag isn't set. I will produce a multidimensional matrix of results, that should be analysed to see if results are conform to what expected.

like image 75
Charles Brunet Avatar answered Sep 28 '22 07:09

Charles Brunet


When I write apps (in scripting languages), I have a function that parses a command line string. I source the file that I'm developing and unit test that function directly rather than involving the shell.

like image 28
Noufal Ibrahim Avatar answered Sep 28 '22 08:09

Noufal Ibrahim