1

When I try to get argument with flag "--from" from argparse.ArgumentParser.parse_args() an error occurs. IDE says that "from" is import statement and the code is unreachable:

parser = argparse.ArgumentParser(prog='cache_wiki.py',
                                     description='Find shortest path between links')
parser.add_argument('--from', required=True, help='page to start search with')
args = parser.parse_args()
print(args.from)

It is ok with another name:

parser = argparse.ArgumentParser(prog='cache_wiki.py',
                                     description='Find shortest path between links')
parser.add_argument('--f', required=True, help='page to start search with')
args = parser.parse_args()
print(args.f)

but I really need to use flag "--from".

2

1 Answer 1

3

I would ignore the IDE here. True, you cannot use args.from, but that's just a syntactic limitation. You can still access the attribute using, for example, getattr(args, 'from').

You can also override the default destination name so that you can use the option --from, but set a different attribute:

...
parser.add_argument('--from',
                    required=True,
                    dest='from_',
                    help='page to start search with')

args = p.parse_args(['--from', 'foo'])
assert args.from_ == 'foo'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.