pythoncommand-lineoptionparser

How can I use OptionParser to print to the command line?


I'd like to use option parser to print the result of a computation to the command line. So far, I have

parser = OptionParser()
parser.add_option('-s','--some', help = "Print some of the Suspects")
parser.add_option('-a','--all',help = "Print all of the Suspects")

(opts,args) = parser.parse_args()

If the user passes -s, I would like the first 25 rows of a dataframe to be printed (I know how to do this). If -a is passed, I would like the entire dataframe to be printed. What do I have left to do?


Solution

  • from optparse import OptionParser
    
    parser = OptionParser()
    parser.add_option('-s','--some', help = "Print some of the Suspects")
    parser.add_option('-a','--all',help = "Print all of the Suspects")
    
    (opts,args) = parser.parse_args()
    
    if opts.some:
        print "some results"
    if opts.all:
        print "all results"