Home > database >  Get two strings by parse one command line argument
Get two strings by parse one command line argument

Time:08-15

I want to turn a command line like this

python3 abc.py abc.csv CC

into strings abc.csv and CC

but I'm not sure if I should use the parse_args() once or call it twice at the main(), as demonstrated below

def parse_args( arg ) :
  
    parser = ArgumentParser()
    parser.add_argument( "file", help = "path to csv file" )
    return parser.parse_args( arg )


if __name__ == "__main__" :

    path = parse_args( sys.argv [ : - 3 ] )
    
    abv = parse_args( sys.argv [ - 2 : ] )

    func( path, abv )

where func() take two strings. Right now it doesn't seem to be working.

CodePudding user response:

parse_args() will typically be called only once with no arguments:

def parse_args():
    parser = ArgumentParser()
    parser.add_argument("file", help="path to csv file")
    parser.add_argument("abv")
    return parser.parse_args()  # same as parse_args(sys.argv[1:])


if __name__ == "__main__":
    args = parse_args()
    func(args.file, args.abv)
  • Related