简体   繁体   中英

python set arguments option for function in command line

I have written simple function to cut lines in txt file, takes 3 arguments :

cut(inicial_line, final_line, file)

now how do i put inicial_line and final_line to -c option to execute it for example:

$ python cut.py -c 5 8 f.txt

and it print file text from 5th to 8th line

i found solution

 __name__=='__main__': import argparse import sys pars = argparse.ArgumentParser() pars.add_argument('-n', nargs=2, type=int) args=pars.parse_args(sys.argv[1:4]) print cut(args.n[0], args.n[1], sys.argv[4]) 

main problem was args=pars.parse_args(sys.argv[1:4])

i didnt know that args=pars.parse_args() is the same as args=pars.parse_args(sys.argv[1:])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM