top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Determine actually given command line arguments

0 votes

I am writing a program that gets its parameters from a combination of config file (using configparser) and command line arguments (using argparse). Now I would also like the program to be able to _write_ a
configparser config file that contains only the parameters actually given on the commandline. Is there a simple way to determine which command line arguments were actually given on the commandline, i.e. does argparse.ArgumentParser() know which of its namespace members were actually hit during parse_args().

I have tried giving the arguments default values and then looking for those having a non-default value but this is really awkward, especially if it comes to non-string arguments. Also, parsing sys.argv looks clumsy because you have to keep track of short and long options with and without argument etc. i.e. all things that I got argparse for in the first place.

posted May 15, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

4 Answers

0 votes
Best answer

Have you looked into docopt? It's pretty awesome, and might really help in this case.

answer May 15, 2013 by anonymous
+1 vote

I don't know about that but I imagine that you could compare values with their defaults to see which have been changed.

answer May 15, 2013 by anonymous
Yes, I was trying that and it sort of works with strings if I use something sufficiently improbable like "__UNSELECTED__" as default. But it gets difficult with boolean or even number arguments where you just may not have valid "improbable" defaults. You could now say, so what, it's the default anyway. But in my program I would like to distinguish between given and not given arguments rather than between default and
Initialize all your arg variables to None, then after command line processing, any which remain as None weren't set on the command line. At that point, set them to the actual defaults. I think that's a
pretty common idiom.

Note: I am an old cranky dude and still use getopt. This idiom ispretty easy there. YMMV with argparse or optparse.
Unfortunately, argparse wants to know the type of the argument and the boolean arguments (those with action=store_true) can't be initialized with None.

However, maybe I could convert boolean arguments to something like
parser.add_argument('--foo', type=str, nargs='?', const='True', default=None)

I'd then have to check for string 'True' rather than for boolean True, though.
0 votes

I think what you're looking for is sys.argv:

$ cat
import sys
print sys.argv

$ python foo bar
['', 'foo', 'bar']

answer May 15, 2013 by anonymous
0 votes

That's what the program does at the moment. However, I'm not quite happy with it. Generally, the user doesn't know what's the default and it would be confusing if 'prog -i 3' doesn't make 'iopt = 3' turn up in
the file at the end. There may also be the case when the user (for whatever reason) _wants_ the default in the file.

I think I will try the opposite instead: the program writes the whole set of options to the file. This would produce a complete and consistent configuration which automatically reflects the hierarchy in which the options were set. And the user can sort it out by hand if he wants.

answer May 16, 2013 by anonymous
Similar Questions
+1 vote

Is it possible to pass command line arguments to C programs? If yes, can you write down the prototype for main function with command line arguments?

0 votes

I have program which takes special character as parameter in command line for a program in java.
But I can not send * (asterisk) as input for my program.

Give me some explain and solution for my problem.

+1 vote

Does there exist any way to make the command line arguments available to other functions without passing them as arguments to the function in C programming?