-
Notifications
You must be signed in to change notification settings - Fork 27
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
refactor: switch from optparse to argparse #38
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned about the use of unsupported interfaces.
@@ -5,3 +5,7 @@ | |||
""" | |||
|
|||
from .cogapp import Cog as Cog, CogUsageError as CogUsageError, main as main | |||
|
|||
|
|||
if __name__ == "__main__": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was here to enable calling the app as python -m cogapp <cli_args>
. https://docs.python.org/3/library/__main__.html#idiomatic-usage
I used it while testing out the --help
output and figured it would be useful to keep. I'm fine taking it out if you don't want it there though.
@@ -801,6 +592,8 @@ def main(self, argv): | |||
except CogError as err: | |||
self.prerr(err) | |||
return 1 | |||
except SystemExit as exit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you need to add this?
cogapp/options.py
Outdated
|
||
|
||
if sys.version_info >= (3, 8): | ||
from argparse import _ExtendAction |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems odd to have to import or duplicate a private class from argparse. Isn't there a supported way to do what we need?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The supported way to do it is by passing the string "extend", but that's only available starting in Python 3.8. I'll refactor to use the string in that case though.
cogapp/options.py
Outdated
width=HELP_WIDTH, | ||
) | ||
|
||
def _split_lines(self, text, width): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again we seem to be reaching into internal details. What is the advantage of using a supported module if we have to use unsupported interfaces in it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have to necessarily. I wasn't thinking of it as problematic but you make a good point. It was purely for aesthetic reasons to add the blank line between option descriptions (which becomes more aesthetically pleasing once there are long options, e.g. see https://github.com/vergenzt/cog/blob/bd3cb2435419906c90ddd08febe8a7df7544260a/docs/running.rst).
I'll remove it though - it's probably not worth the hassle.
getattr(ns, self.dest).update([arg]) | ||
|
||
|
||
class Markers(NamedTuple): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid NamedTuple if we can, won't a dataclass work?
@@ -1046,7 +1053,7 @@ def test_print_output(self): | |||
make_files(d) | |||
self.cog.callable_main(["argv0", "-rP", "test.cog"]) | |||
self.assertFilesSame("test.cog", "test.out") | |||
output = self.output.getvalue() | |||
output, errput = self.capsys.readouterr() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throughout: if we aren't using errput
, we should assign output, _ = ...
, or we should make assertions about errput.
_parser: ClassVar = CogArgumentParser( | ||
prog="cog", | ||
usage=argparse.SUPPRESS, | ||
description=rewrap(description), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we don't use rewrap?
c4e774c
to
9db093e
Compare
Sorry gtg for now - will reply to remainder ASAP |
To consolidate options definitions into a single place, and use the more modern Python option parsing library.