Thread: Strategies for Handling Command Line Arguments

  1. #1
    Registered User
    Join Date
    Aug 2005

    Strategies for Handling Command Line Arguments

    I have a number of command line programs that take a variety of inputs. Depending on the inputs the program functions differently. For example, the program could take A and C as inputs and produce B and D as outputs. Alternatively, the program could take B and D as inputs and produce A and C as outputs. As another alternative, the program could take A and D as inputs and produce B and C as outputs.

    The thing is that I have seven inputs and a handful of outputs. The problem is that the part of my program that handles user input is very messy. I don't have any particularly good strategy to fix this. Has anyone dealt with this issue before? Are there any good ways to clean up the mess?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    How about something like compiler switches?
    foo -iInFile1 -oOutFile -iInFile2 -iInFile3
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Can you explain this a bit more? Why is it "messy"? As you describe it, it sounds fairly straightforward... Ask the user for the inputs, and then ask the user for outputs...

    Then, use if-statements (or a switch statement) to make the program function correctly with the given conditions.

    Does all of this information need to be entered on the command-line when the program is run? Or, is there a user-input "screen" (or two)?

    Are the input and output selections limited, so that you can use menus? In that case, you can create input and output menus, and the user can enter a number or letter to select a menu item(s). Can the user actually enter "A" and/or "B", or is that just a symbolic example?

    Or, is the user entering more free-form data, like file-names?

    In any case, you should check for invalid input and allow the user to re-try. (Sometimes I'll have the program make a "default" selection, if the user enters something invalid.)
    Last edited by DougDbug; 04-08-2008 at 02:45 PM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    What I am using is something like compiler switches. It's essentially
    foo A S D F G -i Q -o W -f E
    By messy, I am referring to the way that all of these inputs are handled within the program. (And yes, the letters "A" and "B" where symbolic, although it doesn't make much difference.) This gets messy, because the variables are used to produce new variables, and then the switches tell the program how to use the variables. My code works okay, I just would not describe it as "clean," "pretty," or "elegant."

  5. #5
    The larch
    Join Date
    May 2006
    So the messiness results from one program doing many things? Or what makes you think it's the command line arguments that make it messy?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Aug 2005
    I can see the messiness in the code. It results from the way the input variables are handled, the error checking of the input variables, and the multiple things that the program can do.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    If you are UNIX, have you looked at getopt()? (man 3 getopt)

    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Apr 2008
    I prefer to seperate the handling of the command-line options from the main program. As an example, I created a program that took a bitmap picture, and sliced it up into tiles (of any size), then saved the resulting tile-map to a new bitmap file, while having the option to create a .map and to go into verbose-mode.

    So I created an OPTION structure to hold all the command-line options / switches first:

    typedef struct Options
    	char	*source,	// source filename
    		*tiles,	// tile bmp filename
    		*map;	// map filename
    	int	x,	// tiles width
    		y;	// tiles height
    	BOOL	v;	// verbose (on/off)
    } OPTIONS;
    Then, I created the function that scans the command-line and fills in the OPTION structure:

    void OPT_Parse (int argc, char *argv[], OPTIONS *opt)
    	int i = 2;
    	/* Clear the opt structure
    		before we fill it in. */
    	memset (opt, 0, sizeof (OPTIONS));
    	/* See if there even are
    		any arguments given. */
    	if (argc > 1)
    		/* The first argument is
    			always the source file. */
    		opt->source = argv[1];
    		while (++ i <= argc)
    			/* Argument -c creates a tile BMP. */
    			if (! (strncmp (argv[i - 1], "-c", 2)))
    				opt->tiles = argv[i ++];
    			/* Argument -m creates a map file. */
    			else if (! (strncmp (argv[i - 1], "-m", 2)))
    				opt->map = argv[i ++];
    			/* Argument -x sets the tile width. */
    			else if (! (strncmp (argv[i - 1], "-x", 2)))
    				opt->x = atoi (argv[i ++]);
    			/* Argument -y sets the tile height. */
    			else if (! (strncmp (argv[i - 1], "-y", 2)))
    				opt->y = atoi (argv[i ++]);
    			/* Turn on verbose mode. */
    			else if (! (strncmp (argv[i - 1], "-v", 2)))
    				opt->v = TRUE;
    		/* If no tile width- or height was
    			specified, we use the default values. */
    		if (! opt->x)
    			opt->x = 8;
    		if (! opt->y)
    			opt->y = 8;
    This has an added bonus that the order of the commands and/or switches do not matter, the only thing it expects is that the first argument is the source filename, which is why I declared the index variable i as 2, as argv[2] is the first place an option should exist.

    Then the main code can check the OPTIONS structure and do things accordingly.

    Hope this helps a bit.
    Last edited by OogyBoogy; 04-09-2008 at 08:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM