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 ;)
Related
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.
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
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:])
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.
The sys.stdin.readline() waits for an EOF (or new line) before returning, so if I have a console input, readline() waits for user input. Instead I want to print help and exit with an error if there is nothing to process, not wait for user input.
Reason:
I'm looking to write a python program with command line behaviour similar to grep.
Test cases:
No input and nothing piped, print help
$ argparse.py
argparse.py - prints arguments
echo $? # UNIX
echo %ERRORLEVEL% # WINDOWS
2
Command line args parsed
$ argparse.py a b c
0 a
1 b
2 c
Accept piped commands
$ ls | argparse.py
0 argparse.py
1 aFile.txt
parseargs.py listing:
# $Id: parseargs.py
import sys
import argparse
# Tried these too:
# import fileinput - blocks on no input
# import subprocess - requires calling program to be known
def usage():
sys.stderr.write("{} - prints arguments".fomrat(sys.argv[0])
sys.stderr.flush()
sys.exit(2)
def print_me(count, msg):
print '{}: {:>18} {}'.format(count, msg.strip(), map(ord,msg))
if __name__ == '__main__':
USE_BUFFERED_INPUT = False
# Case 1: Command line arguments
if len(sys.argv) > 1:
for i, arg in enumerate(sys.argv[1:]):
print_me( i, arg)
elif USE_BUFFERED_INPUT: # Note: Do not use processing buffered inputs
for i, arg in enumerate(sys.stdin):
print_me( i, arg)
else:
i=0
##### Need to deterime if the sys.stdin is empty.
##### if READLINE_EMPTY:
##### usage()
while True:
arg = sys.stdin.readline() #Blocks if no input
if not arg:
break
print_me( i, arg)
i += 1
sys.exit(0)
grep can work the way it does because it has one non-optional argument: the pattern. For example
$ grep < /dev/zero
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
even though there was infinite input available on stdin, grep didn't get the required argument and therefore complained.
If you want to use only optional arguments and error out if stdin is a terminal, look at file.isatty().
import sys,os
print os.fstat(sys.stdin.fileno()).st_size > 0
Calling script
c:\py_exp>peek_stdin.py < peek_stdin.py
True
c:\py_exp>peek_stdin.py
False
You may want to check getopt module. Basic example:
import getopt
import sys
def main(argv):
try:
opts, args = getopt.getopt(argv, "has:f:") # "has:f:" are the arguments
except getopt.GetoptError:
print "print usage()"
sys.exit(1)
if not opts and not args:
print "print usage()"
sys.exit(1)
print "args passed", opts, args
if __name__ == "__main__":
main(sys.argv[1:])
~> python blabla.py
print usage()
~> python blabla.py -a arg
args passed [('-a', '')] ['arg']
~> python blabla.py -b as ----> this fails because -b is not defined for getopt at second parameter
print usage()
What about this one:
#!/usr/bin/env python
import getopt
import sys
import select
def main(argv):
try:
opts, args = getopt.getopt(argv, "has:f:") # "has:f:" are the arguments
except getopt.GetoptError:
print "print usage()"
sys.exit(1)
if not opts and not args:
a, b, c = select.select([sys.stdin], [], [], 0.2)
if a:
itera = iter(a[0].readline, "")
for line in itera:
data = line.strip()
print data
else:
print "print usage()"
print "args passed", opts, args
if __name__ == "__main__":
main(sys.argv[1:])
select.select helps to check if there is data coming
:~> ./hebele.py
print usage()
args passed [] []
:~> ping www.google.com | ./hebele.py
PING www.google.com (173.194.67.105) 56(84) bytes of data.
64 bytes from blabla (173.194.67.105): icmp_seq=1 ttl=48 time=16.7 ms
64 bytes from blabla (173.194.67.105): icmp_seq=2 ttl=48 time=17.1 ms
64 bytes from blabla (173.194.67.105): icmp_seq=3 ttl=48 time=17.1 ms
^CTraceback (most recent call last):
File "./hebele.py", line 25, in <module>
main(sys.argv[1:])
File "./hebele.py", line 17, in main
for line in itera:
KeyboardInterrupt
:~> ls | ./hebele.py
Aptana_Studio_3
Desktop
...
workspace
args passed [] []
:~> ./hebele.py -a bla
args passed [('-a', '')] ['bla']
:~> ./hebele.py sdfsdf sadf sdf
args passed [] ['sdfsdf', 'sadf', 'sdf']