Skip to content

Improve parser #25

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

Merged
merged 7 commits into from
Jan 6, 2017
Merged

Improve parser #25

merged 7 commits into from
Jan 6, 2017

Conversation

mixxorz
Copy link
Collaborator

@mixxorz mixxorz commented Dec 6, 2016

Improve parser

  • Add behave short options to parser

e.g. -d for --dry-run, -e for --exclude

  • Fix specifying paths bug

Before this PR, specifying a path will not work since our parser didn't have an
option for positional argument. This PR adds a paths argument.

  • Customize parser's usage and description text to match behave's

The default usage and description was very messy. This PR updates them to be
like behave's.

  • Add short option (-k) to --keepdb argument

* Add behave short options to parser
* Fix specifying paths bug
* Customize parser's usage and description text to match behave's
* Add short option (-k) to --keepdb argument
* Added :code:`--keepdb` short form argument, `-k`

**Bugfixes**
* Fixed specifying paths didn't work
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a blank link missing after "Bugfixes", otherwise the bulleted list is not interpreted correctly.

Also, please use - signs instead of * for bullets, to keep the markup uniform across the document. 👍

'--version',
'-c',
'-k',
'-v'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be picky, can we put a , (comma) at the end of each list item if it's on a single line? This way we never have changes in existing lines when elements are added.

# Do not add conflicting options
if long_option in conflicts:
# No use in adding an option if it doesn't have an option string
if len(option_strings) == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about ... instead?

if not option_strings:

I have been told this is more pythonic.

Also, consider rewording the comment above to make it sound more positive. Or, even better, omit it if the code is self-explanatory.

Copy link
Collaborator Author

@mixxorz mixxorz Dec 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, what do you think about renaming the long option strings with --behave instead of outright removing them?

E.g. --behave-version

This will only apply to commands where both the short and long option strings are conflicting.

Copy link
Member

@bittner bittner Dec 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that means prefixing only the conflicting options then yes, I agree. 👍


[pytest]
[tool:pytest]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Shouldn't this be [pytest] according to the py.test documentation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest pytest gives me a deprecation warning. Here's their PR for reference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As soon as a new version of behave is released we could actually consider moving the whole content of setup.cfg to tox.ini.

All tools we use for testing also read their sections out of tox.ini, including behave in its development version.

@mixxorz
Copy link
Collaborator Author

mixxorz commented Dec 10, 2016

  • Prefix conflicting options with --behave

if hasattr(keywords.get('type'), '__call__'):
del keywords['type']

# config_help isn't a valid keyword for make_option
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is misleading: make_option is not used anymore. This is a leftover from the implementation with optparse. (Sorry, probably my fault.) -- Is this check still necessary, btw?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is. Seems to work without it.

behave_args.append(option.replace('--behave', ''))
else:
# Strip behave prefix from long options
behave_args.append(option.replace('behave-', ''))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is hard to read. We need comments to explain the code (which seems to support my impression).

How about:

    if option.startswith('--behave-'):
        option = option.replace('--behave-', '')
        prefix = '-' if len(option) == 1 else '--'
        behave_args.append(prefix + option)

There may be a better way.


**Features and Improvements**

- Behave's short form arguments are now accepted (e.g. :code:`-i` for :code:`--include`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add:

- Prefix conflicting command line options with :code:`--behave`

if 'config_help' in keywords:
del keywords['config_help']
# Build option strings
# Conflicting option strings get prefixed with `--behave`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about moving this specific comment two lines down? -- We've got a superabundance of comments here.

if option.startswith('--'):
option = '--behave-' + option[2:]
else:
option = '--behave-' + option[1:]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second level of if, beside (visually) increasing complexity, is visually non-DRY.

How about:

        if option in conflicts:
            option = '--behave-' + option[2 if option.startswith('--') else 1:]

or -- probably more readable and self-explanatory:

        if option in conflicts:
            prefix = '--' if option.startswith('--') else '-'
            option = option.replace(prefix, '--behave-', maxreplace=1)

@mixxorz
Copy link
Collaborator Author

mixxorz commented Dec 12, 2016

  • Improve readability

for option in unknown:
# Remove behave prefix
if option.startswith('--behave-'):
option = option.replace('--behave-', '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should add a 1 (maxreplace) parameter here too, to be on the safe side. Sorry!

Copy link
Member

@bittner bittner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment r91956551 should be addressed, then we're all set.

Copy link
Member

@bittner bittner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ready for merging, I would say. Go for it!

description = """\
Run a number of feature tests with behave."""
parser.usage = usage
parser.description = description
Copy link
Member

@bittner bittner Dec 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, is there a reason why you declare usage and description, only to assign them immediately afterwards? Would a direct assignment not be good enough? Just wondering why, really.

        parser.usage = "%(prog)s [options] [ [DIR|FILE|FILE:LINE] ]+"
        parser.description = """\
            Run a number of feature tests with behave."""

(Sorry for the never-ending story! I promise to stop looking at the code now!) 😏

@mixxorz
Copy link
Collaborator Author

mixxorz commented Dec 24, 2016

Sorry for the delay. I got busy, then got sick, and now it's Christmas. Haha. I'll get back to this as soon as I have some free time. 👍

@mixxorz
Copy link
Collaborator Author

mixxorz commented Jan 6, 2017

Hopefully this is the commit that closes this PR! 😂

@bittner bittner merged commit 278d1e2 into master Jan 6, 2017
@bittner
Copy link
Member

bittner commented Jan 6, 2017

A new release would be nice now.

@mixxorz mixxorz deleted the improve-parser branch January 24, 2017 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants