python - Can't pass values via command line -
i have write program reads file , writes analysis text file. program has take information via command line can't see, figure out given template. wrote test program see if succesfully pass command line input class.
#!/usr/bin/env python3 ######################################################################## # commandline ######################################################################## class commandline() : ''' handle command line, usage , requests. commandline uses argparse, standard in 2.7 , beyond. implements standard command line argument parser various argument options, standard usage , help, , error termination mechanism do-usage_and_die. attributes: arguments received commandline using .add_argument avalable within .args attribute of object instantiated commandline. example, if mycommandline object of class, , requiredbool set option using add_argument, mycommandline.args.requiredbool name option. ''' def __init__(self, inopts=none) : ''' commandline constructor. implements parser interpret command line argv string using argparse. ''' import argparse self.parser = argparse.argumentparser(description = 'program prolog - brief description of thing does', epilog = 'program epilog - other stuff feel compelled say', add_help = true, #default true prefix_chars = '-', usage = '%(prog)s [options] -option1[default] <input >output' ) self.parser.add_argument('infile', action = 'store', help='input file name') self.parser.add_argument('outfile', action = 'store', help='output file name') self.parser.add_argument('-lg', '--longestgene', action = 'store', nargs='?', const=true, default=true, help='longest gene in orf') self.parser.add_argument('-mg', '--mingene', type=int, choices= range(0, 2000), action = 'store', help='minimum gene length') self.parser.add_argument('-s', '--start', action = 'append', nargs='?', help='start codon') #allows multiple list options self.parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1') if inopts none : self.args = self.parser.parse_args() else : self.args = self.parser.parse_args(inopts) ######################################################################## #main goes here ######################################################################## def main(mycommandline=none): ''' implements usage exception handler can raised anywhere in process. ''' mycommandline = commandline(mycommandline) #mycommandline.args.infile #has input file name #mycommandline.args.outfile #has output file name #mycommandline.args.longestgene #is true if longest gene desired #mycommandline.args.start #is list of start codons #mycommandline.args.mingene #is minimum gene length include print (mycommandline.args) # print parsed argument string .. there nothing better if mycommandline.args.longestgene: print ('longestgene is', str(mycommandline.args.longestgene) ) else : pass class test: def __init__(self): print(mycommandline.args.mingene) if __name__ == "__main__": main() class test: def __init__(self): self.test() def test(self, infile = mycommandline.args.infile, outfile = mycommandline.args.outfile, longest = mycommandline.args.longestgene, start = mycommandline.args.start, min = mycommandline.args.mingene): print(infile) print(outfile) print(longest) print(start) print(min) new_obj = test()
the command line input should like: python testcommand.py -ming 100 -longestg -starts atg tass2orfdata-atg-100.txt
supposedly main program goes says "main goes here" when tried got error "mycommandline not defined". moved program end. error 'the '>' operator reserved future use"
i'm using powershell if matters. how data class?
you don't need commandline class. suggested james mills, here example:
import argparse class test: def __init__(self, infile, outfile, longest, start, min): self.infile = infile self.test() def test(self): print(self.infile) def main(): parser = argparse.argumentparser(description = 'program prolog', epilog = 'program epilog', add_help = true, #default true prefix_chars = '-', usage = 'xxx') parser.add_argument('-i', '--infile', action = 'store', help='input file name') parser.add_argument('-o', '--outfile', action = 'store', help='output file name') parser.add_argument('-lg', '--longestgene', action = 'store', nargs='?', const=true, default=true, help='longest gene in orf') parser.add_argument('-mg', '--mingene', type=int, choices= range(0, 20), action = 'store', help='minimum gene length') parser.add_argument('-s', '--start', action = 'append', nargs='?', help='start codon') #allows multiple list options parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1') args = parser.parse_args() test = test(args.infile, args.outfile, args.longestgene, args.mingene, args.start) if __name__ == '__main__': main()
Comments
Post a Comment