I am working on s code that i want to add command line arguments to. I have read the docs.python page on this subject 3 or 4 times and it still has me conffused. I want to be able to set my base directory, change a send and recive email adress if needed , change the list that a file gets posted to, and change the password to the email adresses login. I have a base that im startign from but dont know if its even right if someone could help me figure up how to do all of these it would be of emmence help. Thanks

import argparse

parser = argparse.ArgumentParser(description='Convert SOR to FACTS update and OSG update.')
parser.add_argument('-b', help='The base path', requiered=False, dest='basePath', metavar='Base directory path',default='/home/hatterx/Desktop'
args = paser.pasrse_args()

The best thing to do is to add print(args) to your script and test it in a console with commands such as

$ python filename.py -h
$ python filename.py -b /home/foobar

I would add a long name such as '--basepath' to the '-b' option and use BASEPATH as metavar.

Also I don't think passing a password as a command line option is a good idea, because OSes keep history of the terminal commands.

Finally, Doug Hellmann is always a good reference when it comes to standard library modules.

when i run the one that i put in the top i get the error and the password one is if the sending emails pasword would ever need to be changed so instead of going into my code to change it i could do it from the command line

TypeError:init() got an unexpected keyword argument'requiered'

ok so the first argument works but i can not figure out what to do to get the other ones to work.

import argparse

parser = argparse.ArgumentParser(description='Convert SOR to FACTS update and OSG update.')
parser.add_argument('-b', help='The base path', required=False, dest='basePath', metavar='Base directory path',default='/home/hatterx/Desktop')
parser.add_argument('-se', help='Change sending emailaddress', required=False, dest='sendEmail', metavar='Sending Emailaddress', default='')
parser.add_argument('-re', help='Change reciveing emailaddress', required=False,dest='reviceEmail', metavar='Reciveing Emailaddress', default='')
parser.add.argument('-pw', help='Change password', required=False, dest='changePassword', metavar='Change Password', default='')
parser.add_argument('-tl', help='Change list to Test', required=False,dest='listTest', metavar='TEST List', default='TEST List For API Integration')
parser.add.argument('-fl', help='Change list to FACTS', required=False, dest='listFACTS', metavar='FACTS List', default='FACTS Daily Import')
parser.add.argument('-ol', help='Change list to OSG', required=False, dest='listOSG', metavar='OSG List', default='OSG Special Offers Send Monthly')
args = parser.parse_args()
print(args)

ok i did that and im getting a error that says AttributeError: 'ArgumentParser' object had no attribute 'add'

there were a few type errors where i put a . instead of a -.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.