Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parse_options to use argparse instead of the deprecated optparse. #64

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

stevenj
Copy link

@stevenj stevenj commented Jul 12, 2015

Fix 'extra_options' so that meaningful and complete command line options
can be added on invocation.

fixes #54

extra command arguments is strange in existing fabricate.py and i couldnt find a useful example to use them. Also, optparse is deprecated. So in making the extra_options actually useful, i also updates parse_options to argparse.

To use extra_options one needs to pass an array of dictionaries as the parameter to extra_options, like this:

EXTRA_OPTIONS = [
    {'name_or_flags':['-b','--bdir'],'action':'store','default':None,'help':'Override the default Build Directory'},
]

main(extra_options=EXTRA_OPTIONS)

the reason for this is that argparse.add_argument (and optparse.make_option before it) require keyworded parameters to be able to fully use them. The keywords are expressed as the keys to the dictionary, and are automatically extracted into an option list using ** argument expansion.

'name_or_flags' is special, it is for un-keyworded arguments, to define the name or flag of the option, its value is an array of strings, specifying the 'name' or the 'flags' or both. Again it is turned into an argument using * argument expansion.

Im not 100% happy with this method, as its a bit obtuse, but i couldn't see a more straightforward way to achieve it. I don't think this is backward compatible with the old use of 'extra_options' but as far as i could tell it didn't have any good use as it was.

to use your new option, just refer to main.options.\<option\> in the example above that would be:
main.options.bdir

stevenj and others added 2 commits July 12, 2015 18:02
Fix 'extra_options' so that meaningful and complete command line options
can be added on invocation.
Remove extraneous test print statement
@pjz
Copy link
Contributor

pjz commented Jul 13, 2015

👍

@rmatev
Copy link
Collaborator

rmatev commented Sep 6, 2016

Can we have this merged?

By the way, I don't see how this solves #54. I suspect the OP there wants something along the lines of this

python build.py run arg1 --release --named=value

being equivalent to this

python build.py 'run("arg1", release=True, named="value")'

@chriscz
Copy link
Collaborator

chriscz commented Mar 25, 2017

argparse is not available in Python 2.6 (which fabricate supports). We could introduce a dependency on this argparse package, but then fabricate has dependencies outside the standard library.

Instead I propose we fix the existing bugs exposed by the test suite, merge this update, and release fabricate 2.0. What do you think @rmatev?

@rmatev
Copy link
Collaborator

rmatev commented May 2, 2017

@chriscz agreed. Do we drop 2.6 support after fabricate 2.0?

@chriscz
Copy link
Collaborator

chriscz commented May 3, 2017

Yes, this will be part of 2.0 at which point we will drop 2.6 support.

@chriscz
Copy link
Collaborator

chriscz commented Oct 1, 2020

Hey @stevenj It looks like you've added quite few unrelated scripts to this PR. Would you mind if I just merge up to the replacement of optparse with argparse?

@stevenj
Copy link
Author

stevenj commented Oct 3, 2020

no, i don't mind at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Command arguments
4 participants