2

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.

sleepfreak
  • 45
  • 6
  • Does this answer your question? [Simple argparse example wanted: 1 argument, 3 results](https://stackoverflow.com/questions/7427101/simple-argparse-example-wanted-1-argument-3-results) – Vladimir Fokow Aug 15 '22 at 06:12
  • Close but I think my problem is more of mixing two different concepts. – sleepfreak Aug 15 '22 at 06:35

1 Answers1

2

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)
CCXXXI
  • 181
  • 2
  • 5