parser = argparse.ArgumentParser('Utility to display SyncId information.')

parser.add_argument('ids', help='list of ids to dsiplay information related to them.')

becuase if i pass more than one value to the function from commandline , i get error of multiple values being passed saying error: unrecognized arguments: second third
the first one goes through but second and third doesnt, so i want to make a list of first second and third
but it should be alist of first - third like [first, second, third]

Recommended Answers

All 10 Replies

yes i looked at this page already and so far i have to do it this way frm command line
sync -id 58496001 -id 59088021 -id 59475161
that gives me [58496001, 59088021, 59475161]
what i want is to do sync -id 58496001 59088021 59475161 from the terminal commandline for the list like above ...

so far I have been doing parser.add_argument('-id', dest='ids',default=[], help='list of ids to dsiplay information related to them.', type=int, action='append')

Did you try nargs = '*' ?

didnt worked..

well if I remove action='append' the i do get the list as I want parser.add_argument('-id', dest='ids',default=[], help='list of ids to dsiplay information related to them.', type=int) however how do i check if what flag is used, say I have multiple flags....

I dont' understand your problem. Following argparse documentation, it works very well

#!/usr/bin/env python
import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('ids', nargs = '*', help = 'some ids')
    ns = parser.parse_args()
    print(ns)

Called in a terminal, it gives

[krystosan] argums.py uie opop rrtt
Namespace(ids=['uie', 'opop', 'rrtt'])

If you have more issues, please post the command line you want to use, and your expected resulting argparse namespace.

then from commandline it would be
sync.py -id 58496001 59088021 59475161 -p /usr/people/krystosan/ /usr/people/krystosan/Videos

i want to make a check if -p is passed or -id is passed or both are passed..?

This should work

#!/usr/bin/env python
import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--id', nargs = '*', dest = 'ids', help = 'some ids', default = argparse.SUPPRESS)
    parser.add_argument('-p', nargs = '*', dest = 'folders', help = 'some folders', default = argparse.SUPPRESS)
    ns = parser.parse_args()
    print(ns)

With argparse.SUPPRESS, the option's dest will be removed from the namespace if it is missing from the command line. I would stick to the convention of 2 dashes for option names with more than one letter.

well it supresses the variable so which means if i am passing that to another function then its just not their...

so if sync.py -id 58496001 59088021 59475161 and i am not passing anything for folders or sourcePaths then below call to the function will error out

displayInfo(args.ids,args.folders,args.sourcePaths)

below call to the function will error out

You can add some code to handle the missing values, like args.ids = None if necessary.

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.