Getting command line arguments as tuples in python - python

Here is an example of how I would like to call my script:
python script.py -f file1.txt "string1" "string2" -f file2.txt "string3" "string4"
Every file that goes as input will have 2 strings associated with that file. There can be any number of files.
To simplify, I am trying to get a print like this:
('file1.txt', 'string1', 'string2')
('file2.txt', 'string3', 'string4')
Here is what I have so far:
import sys, os, traceback, optparse
import time
import re
#from pexpect import run, spawn
def main ():
global options, args
print options.filename
#for filename in options.filename:
# print filename
#f = file(filename,'r')
#for line in f:
# print line,
#f.close()
if __name__ == '__main__':
try:
start_time = time.time()
parser = optparse.OptionParser(formatter=optparse.TitledHelpFormatter(), usage=globals()['__doc__'], version='$Id$')
parser.add_option ('-f', '--file', dest='filename', help='write report to FILE', metavar='FILE', nargs=3)
parser.add_option ('-v', '--verbose', action='store_true', default=False, help='verbose output')
(options, args) = parser.parse_args()
#if len(args) < 1:
# parser.error ('missing argument')
if options.verbose: print time.asctime()
main()
if options.verbose: print time.asctime()
if options.verbose: print 'TOTAL TIME IN MINUTES:',
if options.verbose: print (time.time() - start_time) / 60.0
sys.exit(0)
except KeyboardInterrupt, e: # Ctrl-C
raise e
except SystemExit, e: # sys.exit()
raise e
except Exception, e:
print 'ERROR, UNEXPECTED EXCEPTION'
print str(e)
traceback.print_exc()
os._exit(1)
With the above script, I get only the second file and related strings:
('file2.txt', 'string3', 'string4')

I think you want to use the action=append argument of the add_argument method
import argparse
parser= argparse.ArgumentParser()
parser.add_argument ('-f', '--file', nargs=3, action='append')
files = parser.parse_args('-f file1 string1 string2 -f file2 string3 string4 -f file3 string5 string6'.split()).file
for f in files:
print tuple(f)
gives you:
('file1', 'string1', 'string2')
('file2', 'string3', 'string4')
('file3', 'string5', 'string6')
Testing on cli:
with:
import argparse
parser= argparse.ArgumentParser(prog='Test', usage='%(prog)s -f Filename Option1 Option2 ')
parser.add_argument ('-f', '--file', nargs=3, action='append')
files = parser.parse_args().file
for f in files:
print tuple(f)
results:
python test.py -f file1 "foo bar" "baz" -f file2 foo bar
('file1', 'foo bar', 'baz')
('file2', 'foo', 'bar')
python test.py -f file1 "foo bar" "string2" -f file2 foo bar -f file3 "foo" "bar"
('file1', 'foo bar', 'string2')
('file2', 'foo', 'bar')
('file3', 'foo', 'bar')
python test.py -f file1 "foo bar"
usage: Test -f Filename Option1 Option2
Test: error: argument -f/--file: expected 3 argument(s)

argparse supports the notion of accumulators, which allow you to specify the same option more than once, which is probably more like what you want than anything optparse supports (your particular problem is that optparse doesn't know what to do with an argument specified multiple times, so it's "last one wins" at the command line). argparse is included in Python 2.7 and 3.2, but you should be able to download it for anything 2.6.x or later.

You weren't very clear on your constraints, but this will work for any number of -fs and other flags
import getopt
import sys
args = sys.argv[1:]
tuples = []
while args:
try:
opts, args = getopt.getopt(args, "f:v", ["file", "verbose"])
except getopt.GetoptError, err:
print str(err)
sys.exit(-1)
for o, a in opts:
if o in ("-f", "--file"):
tuples.append((a, args.pop(0), args.pop(0)))
if o in ("-v", "--verbose"):
print "yep, verbose"
print tuples

I would approach this similarly to the previous answer with a slight tweak:
import getopt
import sys
args = sys.argv[1:]
tuples = []
while args:
try:
(opts, args) = getopt.getopt(args, "f:v", ["file=", "verbose"])
except getopt.GetoptError, err:
print str(err)
sys.exit(2)
Now you can either require input in the following way:
-f file1.txt,string1,string2
And parse it in the following way:
for opt, arg in opts:
if opt in ("-f", "--file"):
tuples.append(tuple(arg.split(",")))
if opt in ("-v", "--verbose"):
print "yep, verbose"
print tuples
Or design the input as one string:
-f "file1.txt string1 string2"
and split on whatever strikes your fancy.

If you are using optparse because you want to remain compatible with python 2.6, the action='append' solution works, too:
import optparse
parser = optparse.OptionParser()
parser.add_option('-f', '--file', dest='filename', nargs=3, action='append')
Demonstration
>>> (opts, args) = parser.parse_args("-f file1.txt string1 string2 -f file2.txt string3 string4".split())
>>> for fn_tuple in opts.filename:
... print fn_tuple
('file1.txt', 'string1', 'string2')
('file2.txt', 'string3', 'string4')

Related

docopt parsing doesn't return the expected dictionary

I wanna do same cli tools and tried docopt but i struggled many times:
here's my docstring:
"""
usage:
wplan [--progress] [--forced] [--path DIR]
[--verbosity VMODE]
wplan -h
options:
--progress show progress bar
--forced force overwrite
--path DIR ddsfwefafef [default: /path/to/file]
--verbosity VMODE asdfasdf [default: 1]
-h some help
"""
But no one of the following cli strings results in a docopt dictionary:
"wplan --progress" -> no dictionary -> usage screen
"wplan --forced" -> no dictionary -> usage screen
"wplan --verbose" -> no dictionary -> usage screen
Ok - i testet the docopt stuff with that file:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
# to run docopt.docopt in a test: replace DocoptExit in docopt
# func
from docopt import \
DocoptExit, printable_usage, parse_defaults, parse_pattern, \
formal_usage, TokenStream, parse_argv, Option, Dict, \
AnyOptions, extras
def pseudo_docopt(doc, argv=None, help=True, version=None,
options_first=False):
"""Thats a copy of docopt.docopt function. Only the last line
the "DocoptExit()" statement ist replaced by "raise RuntimeError".
"""
if argv is None:
argv = sys.argv[1:]
DocoptExit.usage = printable_usage(doc)
options = parse_defaults(doc)
pattern = parse_pattern(formal_usage(DocoptExit.usage), options)
argv = parse_argv(TokenStream(argv, DocoptExit), list(options),
options_first)
pattern_options = set(pattern.flat(Option))
for ao in pattern.flat(AnyOptions):
doc_options = parse_defaults(doc)
ao.children = list(set(doc_options) - pattern_options)
extras(help, version, argv, doc)
matched, left, collected = pattern.fix().match(argv)
if matched and left == []: # better error message if left?
return Dict((a.name, a.value)
for a in (pattern.flat() + collected))
# if code goes here no dict is sent
raise RuntimeError()
# some command line tests
cltests = [
[],
# 1 arg
["--progress"],
["--forced"],
# 2 args
["--progress", "--forced"],
["--forced", "--progress"],
["--path /path/to/file"],
["--verbosity", "2"],
# 3 args
["--progress", "--path", "/path/to/file"],
["--forced", "--path", "/path/to/file"],
["--progress", "--verbosity", "2"],
["--forced", "--verbosity", "2"],
# 4 args
["--forced", "--progress", "--path", "/path/to/file"],
["--progress", "--forced", "--path", "/path/to/file"],
["--forced", "--progress", "--verbosity", "2"],
["--progress", "--forced", "--verbosity", "2"],
]
# the __doc__ fake
doc = """
usage:
wplan [--progress] [--forced] [--path DIR]
[--verbosity VMODE]
wplan -h
options:
--progress show progress bar
--forced force overwrite
--path DIR ddsfwefafef [default: /path/to/file]
--verbosity VMODE asdfasdf [default: 1]
-h some help
"""
for args in cltests:
cmd = ["wplan", ] + args
info = "run {}".format(cmd)
print(info + os.linesep + "-" * len(info))
try:
data = pseudo_docopt(doc=doc, argv=cmd)
assert isinstance(data, dict)
assert "--progress" in data
assert "--progress" in data
assert "--forced" in data
assert "--path" in data
assert "--verbosity" in data
print("... OK there is a docopt dict")
except RuntimeError:
print("... ERROR: no docopt dict for: {}".format(cmd))
print()
So if i run this docoptest.py file ("python3.7 docopttest.py") only the ERROR info appears!
First two actually work, here are the live demos:
wplan --progress
wplan --forced
The last needs to be modified to match the usage pattern, e.g. like this:
wplan --verbosity 3
Reading your code, I think this will avoid the need for that pseudo_docopt function, allowing use of the real docopt:
for args in cltests:
... # same
try:
data = docopt.docopt(doc=doc, argv=cmd)
... # assert, print... same
except DocoptExit:
print("... ERROR: no docopt dict for: {}".format(cmd))
Note that DocoptExit inherits from SystemExit which inherits form BaseException, so using just except Exception won't work.

Python getopt not setting correct value

It could be I'm completely misunderstanding the getopt module
I am trying to parse [--magic-m] to my program, but it does not set the correct field.
Part of Encrypt Function
def encrypt(filename, text, magic):
if not magic is None:
hash = pbkdf2_sha256.encrypt(magic, rounds=10000, salt_size=16)
print pbkdf2_sha256.verify(magic, hash)
try:
d = load_image( filename )
except Exception,e:
print str(e)
Part of Load function
def load_image( filename ) :
img = Image.open( os.path.join(__location__, filename) )
img.load()
data = np.asarray( img, dtype="int32" )
return data
Main
if __name__ == "__main__":
if not len(sys.argv[1:]):
usage()
try:
opts,args = getopt.getopt(sys.argv[1:],"hedm:",["help", "encrypt", "decrypt", "magic="])
except getopt.GetoptError as err:
print str(err)
usage()
magic = None
for o,a in opts:
if o in ("-h","--help"):
usage()
elif o in ("-e","--encrypt"):
to_encrypt = True
elif o in ("-d","--decrypt"):
to_encrypt = False
elif o in ("-m", "--magic"):
magic = a
else:
assert False,"Unhandled Option"
print magic
if not to_encrypt:
filename = sys.argv[2]
decrypt(filename, magic)
else:
filename = sys.argv[2]
text = sys.argv[3]
encrypt(filename, text, magic)
I tried calling the program above like this:
[1] python stego.py -e test.jpeg lol -m h
or like this:
[2] python stego.py -e -m h test.jpeg lol
Output becomes:
[1] None
[2] lol
[2] True
[2] [Errno 2] No such file or directory: 'C:\\Users\\Educontract\\Steganography\\-m'
Whitout the option -m everything works fine
The colon should come after m to indicate that it requires an argument. You should also include an equals sign after the long option magic to indicate that it requires an argument.
getopt.getopt(sys.argv[1:],"hedm:",["help", "encrypt", "decrypt", "magic="])
You should put all your options before the arguments, as in your second example.
python stego.py -e -m h test.jpeg lol
If you print sys.argv, I think you'll find that sys.argv[2] and sys.argv[3] are not what you expect. I would fetch the arguments from args, rather than sys.argv.
filename = args[0]
text = args[1]
Note that you may find it easier to use the argparse library instead of getopt. It isn't as strict about requiring options before arguments.

How to switch default True to False on specifying and argument?

#!/usr/bin/env python
import optparse
p = optparse.OptionParser()
p.add_option("-o", action="store", dest="outfile")
p.add_option("-d", action="store_true", dest="debugflag")
p.set_defaults(debugflag=True)
opts,args = p.parse_args()
print opts, " ", args
print opts.outfile, opts.debugflag
Output:
$ ./optparseexample.py -o myfile -d
{'outfile': 'myfile', 'debugflag': True} []
myfile True
$ ./optparseexample.py -o myfile
{'outfile': 'myfile', 'debugflag': True} []
myfile True
Question:
How to I switch the default value for debugflag from True to False ?
You should use action=store_false then.
p.add_option("-d", action="store_false", dest="debugflag")
Please try to read the documentation before asking.

Python argument parsing get wrong result

I have a python script to parse input argument from user .my code is like this
def get_arg(argv):
result = {}
input_file=stag_db=main_tb=stag_table=main_table = "";
debug="N"
msg = ''' Syntax: dt transaction date
-i input_file (E.g. input_file.tar.gz)
-ds staging_database
-dm main_database
-ts staging_table
-tm main_table
-db debug (Y/N)'''
try:
opts, args = getopt.getopt(argv,"hd:i:ds:dm:db:ts:tm:",["ifile=","ofile="])
print args
print opts
except getopt.GetoptError:
f_end_process(msg)
for opt, arg in opts:
if opt == '-h':
f_end_process(msg)
elif opt == "-i":
input_file = arg
elif opt == "-ds":
stag_db = arg
elif opt == "-dm":
main_tb = arg
elif opt == "-ts":
stag_table = arg
elif opt == "-tm":
main_table = arg
elif opt == "-db":
debug = arg
result = {'input_file':input_file,'stag_db':stag_db,'main_tb':main_tb,'stag_table':stag_table,'main_table':main_table}
print result
if '' in result.values():
exit_status=-1
f_end_process(msg)
result['debug']= debug
return result
def main():
global input_arg
input_arg = get_arg(sys.argv[1:])
print "process started at " +strftime("%Y-%m-%d %H:%M:%S")
print input_arg
i am running code like this
python main.py -i ok.txt -ds ds_val -dm dm_val -ts tas_val -tm tm_val
i want to parse all input arguments to a list. i imported all required modules to my script
now i am able to parse only -i input.How can i parse -tm,-ts ,-dm,-ds iputs?
In an interactive Python experiment with passing various argv arrays to getopt
>>> getopt.getopt(['-hd','1'],"hd:i:ds:dm:db:ts:tm:")
([('-h', ''), ('-d', '1')], [])
You did not tell it to look for an -hd option, but rather a -h and a -d that takes an argument.
Generally for multiletter options, argument parsers expect you to use --. Your getopt should be
>>> getopt.getopt(['--hd','1'],"i:",["hd=","ds=",...])
([('--hd', '1')], [])
But, do consider argparse.
You can manage using argparse, in just a few lines.
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input-file', '-i', type=str,
help='input file (eg: input_file.tar.gz)')
parser.add_argument('--staging-db', '-S', type=str,
help='staging database name')
parser.add_argument('--main-db', '-M', type=str,
help='main database name')
parser.add_argument('--staging-table', '-s', type=str,
help='staging table name')
parser.add_argument('--main-table', '-m', type=str,
help='main table name')
parser.add_argument('--debug', '-d', type=bool, default=False,
help="activate debug mode (defaults to False)")
args = parser.parse_args()
The parser help is generated by argparse. You can output it by typing
$ python YOURSCRIPT.py --help

What is the data type returned by getopt in Python

I have following use of getopt in my code:
opts, args = getopt.getopt(sys.argv[1:], "", ["admit=", "wardName="])
Then I run the code from command line in the following matter:
Test.py --args --admit=1 --wardName="CCU"
When I print the contents of opts, I get the following output:
[('--admit', '1'), ('--wardName', 'CCU')]
The first question is what is the data type of this result? It seems to me like a list of tuples. Is that correct?
The second question - is there any convenient way to work with such tuple pairs (if these ARE tuples)? For example, how can I now say: if admit == 1 then do X?
I thought of converting the tuples to a dictionary, but is that a good practice?
P.S. It shouldn't make any difference this is actually Jython and not pure Python.
The front page of the Python docs describes the Python Library docs as "keep this under your pillow". The page on getopt is at:
http://docs.python.org/2/library/getopt.html
You get two lists back from getopt: That list of tuples that you mentioned, followed by the list of remaining arguments after all the options have been parsed out. Try this:
import getopt
args = ['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
opts, args = getopt.getopt(args, "abc:d")
print ("opts=" + str(opts))
print ("args=" + str(args))
optdic = dict(opts) # convert options to dictionary
print ("optdic['-c'] = " + str(optdic['-c']))
output:
opts=[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', '')]
args=['bar', 'a1', 'a2']
optdic['-c'] = foo
A convenient way to work with the options and values:
import getopt, sys
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "", ["admit=", "wardName="])
print opts
except getopt.GetoptError, err:
# print help information and exit:
print str(err)
sys.exit(2)
admitArg = False
wardName = ""
for o, a in opts:
if o == "--admit":
admitArg = a
elif o == "--wardName":
wardName = a
else:
assert False, "unhandled option"
# do something with the argument values
if admitArg:
print "--admit is True"
if wardName == "Foobar":
print "wardName is Foobar!"
if __name__ == "__main__":
main()
Yes, it is a list of tuplea. You can use type() to check.
A dictionary would work fine.
D = dict(opts)
print D['--admit']
print D['--wardName']

Categories

Resources