Arguments for batch size not being captured. Any idea? - python

I do have following script to capture the command line arguments. Third option for batch size is not being set any idea ?
FYI : Only input & ouput file options are required parameter. Batch is optional.
import sys, getopt
import os
try:
opts, args = getopt.getopt(argv,"hi:o:b",["ifile=","ofile=","bsize="])
except getopt.GetoptError:
print 'file.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
sys.exit(2)
for opt, arg in opts:
print opt
if opt == '-h':
print 'file.py -i <inputfile> -o <outputfile> -b<batchsize>[default=20000000]'
sys.exit()
elif opt in ("-i", "--ifile"):
inputFile = arg
elif opt in ("-o", "--ofile"):
outputFile = arg
elif opt in ("-b", "--bsize"):
print "Another option %s" % opt
batchsize = arg

You have several problems with this script including indent errors and failing to initialize settings first... But let's assume you just copy/pasted it incorrectly.
The answer is that you are missing a ":" after the "b" in your getopt arguments.
This line:
opts, args = getopt.getopt(argv,"hi:o:b",["ifile=","ofile=","bsize="])
Should actually be:
opts, args = getopt.getopt(argv,"hi:o:b:",["ifile=","ofile=","bsize="])

If you don't mind, I would recommend argparse. It is better usably and not as clunky as getopts.
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--ifile", required=True)
parser.add_argument("-o", "--ofile", required=True)
parser.add_argument("-b", "--bsize", type=int, default=20000000)
parser.parse_args()
input_file = parser.input
etc.

Related

Pass multiple argument from command line

i trying with
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
dpd = ''
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile=","dpd="])
print(opts)
for opt, arg in opts:
print(opt)
if opt == '-h':
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-d","--dpd"):
dpd = arg
print ('Input file is ', inputfile)
print ('Output file is ', outputfile)
print ('DPD', dpd)
if __name__ == "__main__":
main(sys.argv[1:])
and run with python3 demo.py -i 65 -o ale -d 45 but it's give error
getopt.GetoptError: option -d not recognized
and i want to pass 6 pass argument how can i do this??
You should be using argparse, which is simpler and much more powerful than getopt.
But the problem is that you forgot to declare -d as an option:
opts, args = getopt.getopt(argv,"hi:o:d:",["ifile=","ofile=","dpd="])
There is a built-in library for this in Python. I strongly recommend that you use it - you get a lot of stuff out of the box and the errors will be more informative.
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-d', '--dpd')
>>> args = parser.parse_args(['-d', 'bla'])
>>> args.dpd
'bla'
If you just add the rest of your arguments your program becomes much simpler:
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--ifile')
parser.add_argument('-o', '--ofile')
parser.add_argument('-d', '--dpd')
print(f'dpd: {args.dpd}')
print(f'input: {args.ifile}')
print(f'output: {args.ofile}')
You get the help arg with autoexit out of the box too.
You can also specify which parameters are required and which are optional, set default values and a lot more.

Python , getopt , how to get a list

I'm trying to use the library getopt in order to catch a list of path given from command line something like this :
python script.py -l ["a","b","c","d","e"] -p 80 ....
what I wrote is this:
def getValue(self):
'''
get value from command line and initialize
variable !
'''
try:
opts,args = getopt.getopt( self.args ,
"hl:us:r:p:" ,
['help','local_path','update',"remote_host","remote_path","parameter"])
except getopt.GetoptError as err:
print(str(err))
self.usage()
## ----------------- SETTING VARIABLE PASSED BY COMMAND LINE ------------------ ##
for opt,arg in opts:
#----------------------------------------------------------
if opt in ("-l","--local_path"):
self.local_path = arg
if DEBUG:
print('local path: ',self.local_path)
#-----------------------------------------------------------
elif opt in ("-h", "--help"):
self.usage()
#-----------------------------------------------------------
elif opt in ("-s", "--remote_host"):
self.remote_host = arg
if DEBUG:
print('Simulation host: ', self.remote_host)
#-----------------------------------------------------------
elif opt in ("-r", "--remote_path"):
self.remote_path = arg
if DEBUG:
print('Simulation path: ', self.remote_path)
#-----------------------------------------------------------
elif opt in ("-p", "--parameter"):
self.parameter = arg
if DEBUG:
print('Simulation parameter: ',self.parameter)
#-----------------------------------------------------------
elif opt in ("-u","--update"):
#if self.remote_host and self.remote_path:
self.update()
#-----------------------------------------------------------
else:
assert False, "Unhandled Option"
#-----------------------------------------------------------
but unfortunately this take just single value for each opts (-l, -p ....)
how can reach my aim ?
thanks in advance !!
I've simplified your script a bit to specifically address your question about passing a list of arguments from the command line.
One option you have is to specify the same flag multiple times to send several arguments into your program. For example:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
local_paths = []
for opt, arg in opts:
if opt in ('-l', '--local_path'):
local_paths.append(arg)
print(opt + ': ' + arg)
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
print('local_paths: ' + str(local_paths))
Used as follows:
$ python script.py -la -lb -p 80
-l: a
-l: b
-p: 80
local_paths: ['a', 'b']
Another option (if you must pass the list itself via the command line to a single instance of the flag) is to use some sort of serialization. JSON is up to the task but you could also use csv or others. Example:
import getopt
import json
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
list_arg = json.loads(arg)
print(opt + ': ' + str(list_arg))
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
$ python script.py -l '["a","b"]' -p 80
-l: ['a', 'b']
-p: 80
Note the quotes (') around the JSON after the -l flag ('["a","b"]'). This notation "protects" the argument from being evaluated by bash.
If you pass the argument as you have done in your example, python still receives a single argument but it does not quite work as I think you intend:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:',
['help', 'local_path'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
print(opt + ': ' + arg)
print('type: ' + str(type(opt)))
$ python script.py -l ["a","b"]
-l: [a,b]
type: <class 'str'>
The argument for the -l flag is literally the string "[a,b]" in python. This happens because bash evaluated the expression ["a","b"] before running the script, so now deserialization in python is a bit trickier. It is probably worth avoiding this way and sticking with a standard serialization pattern.
To clarify the notation above from the docs:
shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':')
So hl: means we accept -h and -l, but -l must have an argument, otherwise we will get something like:
getopt.GetoptError: option -l requires argument

How do i use getopt.getopt method in python?

I would like to add warning and critical alert condition if inputfile == "android": pass then check the number stored in variable e and f and check whether it is under normal or warning or critical level as whatever the arguments we passed.
Secondly this script gives no output when run in python3.6
#!/usr/bin/python
import requests, os, json, sys, getopt
f = 10
e = 20
def main(argv):
inputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:w:c:",["ent","lable","help","warning","critical"])
except getopt.GetoptError:
print ("Usage: test.py -i <inputfile>")
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ("test.py -i <inputfile>")
sys.exit()
# elif opt in ("-i", "--app"):
elif opt == '-i':
inputfile = arg
if inputfile == "android":
'''call a function here (hiding sensitive information) Using e and f variable instead'''
print ("Input is"), inputfile
print("Active:"), f
else:
print("Parameter not found")
sys.exit(2)
# elif opt in ("-o", "--lable"):
elif opt == '-o':
inputfile = arg
print("Active:"), e
if __name__ == "__main__":
main(sys.argv[1:])
Right Now:
#python script -i android
Output
Active: 10
Expected:
#python script -i android -w 5 -c 20
Output
WARNING - Active: 10
sys.argv is a list of str. If you want to perform comparisons with the ints you store in e and f, you need to convert the command line parameters during parsing:
if opt == '-i':
input_file = arg
elif opt == '-w':
w_value = int(arg)
elif opt == '-c':
c_value = int(arg)
This will allow to validate them later:
if input_file == 'android':
if w_value < e:
sys.exit('WARNING - w value is too small')
elif c_value > f:
sys.exit('WARNING - c value is too big')
original answer
You might want to have a look at Python's logging module.
To get you started:
import logging
e = 10
f = 20
logging.basicConfig(level=logging.DEBUG)
logging.log(e, 'This is debug output')
logging.log(f, 'This is an info')
This produces:
DEBUG:root:This is debug output
INFO:root:This is an info
To continue from there, you may want to influence the format of the output by passing the format keyword to basicConfig():
e = 30 # logging.WARNING
f = 40 # logging.ERROR
input_file = 'android'
logging.basicConfig(level=logging.DEBUG, format='%(levelname)s - %(message)s')
logging.log(e, 'Input file is "%s"', input_file)
logging.log(f, '"-o" flag detected')
This will produce:
WARNING - Input file is "android"
ERROR - "-o" flag detected
Hope that answers your question,
dtk
PS On a side note: personally I much prefer using argparse over getopt. Fwiw ;)

New to python. Need help to understand code

Hello I am new to python just started . Would be glad if someone can help me to understand this piece of code.
#!/usr/bin/python
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
if __name__ == "__main__":
main(sys.argv[1:])
To the best of my ability, please find my comments (# comments) below:
#!/usr/bin/python
# importing libraries
import sys, getopt
def main(argv):
# defining empty string variables
inputfile = ''
outputfile = ''
# try/except block. Try this code, if it fails, handle it
try:
# looks like you are parsing command line arguments
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit(2)
#iterate through the cmd line arguments
for opt, arg in opts:
# of option is -h, do this (same with the following)
if opt == '-h':
print 'test.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Input file is "', inputfile
print 'Output file is "', outputfile
# python main function
if __name__ == "__main__":
# call method main and pass the cmd line arguments
main(sys.argv[1:])

parse command line arguments not reading all arguments?

So, I came across the getopt module to parse command line args, although I can't make any sense of the docs. For whatever reason, I cannot figure out why this isn't seeing my --domain example.com argument..
$ ./httpdsetup.py -a -u zack --domain example.com
[('-a', ''), ('-u', '')]
I printed out what gets dumped into opts to see what it saw. The code below is just about an exact duplicate from the documentation site.
def main(argv):
import getopt
try:
opts, args = getopt.getopt(argv, "h:al:ud:v", ["user=", "apache", "lighttpd", "dir=", "domain=", "vhost="])
except getopt.GetoptError:
print_usage()
sys.exit(2)
username = ''
directory = ''
domain = ''
httpd = 'apache'
print(opts)
for opt, arg in opts:
if opt == '-h':
print_usage()
sys.exit()
elif opt in ('-u', '--username'):
username = arg
elif opt in ('-d', '--dir'):
directory = arg
elif opt in ('-v', '--domain', '--vhost'):
domain = arg
elif opt in ('-a', '--apache'):
httpd = 'apache'
elif opt in ('-l', '--lighttpd'):
httpd = 'lighttpd'
else:
print_usage()
sys.exit()
if httpd == 'apache':
create_apache_vhost(domain, directory, username)
elif httpd == 'lighttpd':
create_lighty_vhost(domain, directory, username)
if __name__ == '__main__':
main(sys.argv[1:])
I prefer argparse. Python documention here.
It's in Python >=2.7 and >=3.2, but not in Python 3.0 and 3.1. If it's missing in your install, just copy the single file from here to where your script is, or into your Python install.
Here's something close to your example with argparse:
#!/usr/bin/env python3
import sys
def create_apache_vhost(*args, **kwargs):
pass
def create_lighty_vhost(*args, **kwargs):
pass
def main(argv):
import argparse
parser = argparse.ArgumentParser(description="Some server",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--username', type=str)
parser.add_argument('-u', dest='username', type=str)
parser.add_argument('--apache', dest='httpd', action='store_const', const='apache')
parser.add_argument('-a', dest='httpd', action='store_const', const='apache')
parser.add_argument('--lighthttpd', dest='httpd', action='store_const', const='lighthttpd')
parser.add_argument('-l', dest='httpd', action='store_const', const='lighthttpd')
parser.add_argument('--domain', type=str)
parser.add_argument('--vhost', type=str)
parser.add_argument('-v', dest='domain', type=str)
parser.add_argument('--dir', dest='directory', type=str)
parser.add_argument('-d', dest='directory', type=str)
defaults = {
'httpd': 'apache',
}
parser.set_defaults(**defaults)
args = parser.parse_args(args=argv)
print(args)
if args.httpd == 'apache':
create_apache_vhost(args.domain, args.directory, args.username)
elif args.httpd == 'lighttpd':
create_lighty_vhost(args.domain, args.directory, args.username)
if __name__ == '__main__':
main(sys.argv[1:])

Categories