0

hey all
I am trying to write a pogram with arguments and options

I want to call it this way:

python myScript.py -c -f 'C:/Temp/my_file.txt'

OR

python myScript.py -u -f 'C:/Temp/my_file.txt'

where [-f filename] is mandatory
and exactly one of -c or -u should be there

In other words,
Arguments = [-c -u] -f filename (I prefer to drop the -f if possible)

The script copies the file in a destination folder, which is different according to the -c or -u option.

I couldn't write this down.
I used the getOpt module, then the optParse one. I did the [-c -u] part with getopt, but I couldn't add the filename part and validate whether the argument is there..

Please I need some help on this.
I assume it is very easy for those who are familiar with python, but I searched a lot and the documentation about this is very poor, especially when I want to use an option without argument..

Thanks

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by woooee
0

A few print statements should simplify things

import sys

##python ./myScript.py -b -e 'test_name'
print sys.argv, "\n"
file_found=0
for j in range(1, len(sys.argv)):
   print " arg", j, sys.argv[j]
   if not sys.argv[j].startswith("-"):
      print "     name is", sys.argv[j]
      file_found=1
if (" -c" not in sys.argv) and (" -u" not in sys.argv):
   print "\n no -c or -u arg found"
if not file_found:
   print "\n no file name arg found"
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.