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

argparse: add positional=false option #33164

Closed
wants to merge 7 commits into from
Closed

Conversation

liewegas
Copy link
Member

@liewegas liewegas commented Feb 9, 2020

Allow argdesc to contain 'positional=false', which makes an arg not parsed positionally, requiring the --foo[= [value syntax.

Fixes: https://tracker.ceph.com/issues/42113

Replaces #31042
Based on top of #33135

@liewegas
Copy link
Member Author

liewegas commented Feb 9, 2020

@matthewoliver what do you think?

@liewegas
Copy link
Member Author

liewegas commented Feb 9, 2020

Note that I think we can fix https://tracker.ceph.com/issues/42113 in a better way: allow --format to be specified for any command. It's silly that we have to specify it as a param for that one 'orchestrator device ls'.

@matthewoliver
Copy link
Contributor

matthewoliver commented Feb 9, 2020 via email

src/common/cmdparse.cc Outdated Show resolved Hide resolved
@tchaikov
Copy link
Contributor

the comment posted at #33135 also applies.

@liewegas
Copy link
Member Author

hrm, ceph.in chokes on unexpected keys in the description:

Traceback (most recent call last):
  File "bin/ceph", line 1271, in <module>
    retval = main()
  File "bin/ceph", line 1050, in main
    return do_extended_help(parser, childargs, target, ' '.join(childargs))
  File "bin/ceph", line 416, in do_extended_help
    return help_for_target(target, partial)
  File "bin/ceph", line 413, in help_for_target
    return help_for_sigs(outbuf.decode('utf-8'), partial)
  File "bin/ceph", line 391, in help_for_sigs
    sys.stdout.write(format_help(parse_json_funcsigs(sigs, 'cli'),
  File "/home/sage/src/ceph3/src/pybind/ceph_argparse.py", line 847, in parse_json_funcsigs
    cmd['sig'] = parse_funcsig(cmd['sig'])
  File "/home/sage/src/ceph3/src/pybind/ceph_argparse.py", line 797, in parse_funcsig
    **kwargs))
  File "/home/sage/src/ceph3/src/pybind/ceph_argparse.py", line 673, in __init__
    self.instance = self.t(**self.typeargs)
TypeError: __init__() got an unexpected keyword argument 'positional'

src/common/cmdparse.cc Outdated Show resolved Hide resolved
@@ -1024,6 +1085,7 @@ def validate(args, signature, flags=0, partial=False):
validate_one(kwarg_v, kwarg_desc)
matchcnt += 1
store_arg(kwarg_desc, d)
matchcnt += 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Incrementing matchcnt is now redundant, back when the patch was first created it was required. But since I've pushed a PR that has since merged that did just this to fix #33004 (now just needs to be back ported)

@@ -442,7 +442,7 @@ class CephOsdName(CephArgtype):

osd.<id>, or <id>, or *, where id is a base10 int
"""
def __init__(self):
def __init__(self, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

i don't understand this change, as argdesc.__init__() is the only place where we construct these CephArgtype instances. but since this method's signature looks like

   def __init__(self, t, name=None, n=1, req=True, positional=True, **kwargs):

any unknown parameter should be consumed by CephArgtype already. actually after reverting this change and applying following patch

diff --git a/src/pybind/ceph_argparse.py b/src/pybind/ceph_argparse.py
index b8d4664cf8..8782b6a70a 100644
--- a/src/pybind/ceph_argparse.py
+++ b/src/pybind/ceph_argparse.py
@@ -509,7 +509,7 @@ class CephBool(CephArgtype):
     A boolean argument, values may be case insensitive 'true', 'false', '0',
     '1'.  In keyword form, value may be left off (implies true).
     """
-    def __init__(self, strings='', **kwargs):
+    def __init__(self, strings=''):
         self.strings = strings.split('|')

     def valid(self, s, partial=False):

i ran following tests:

$ bin/ceph config set global mon_warn_on_pool_pg_num_not_power_of_two false --force=false
$ bin/ceph config set global mon_warn_on_pool_pg_num_not_power_of_two true
$ bin/ceph orch device ls gnit json
Error ENOENT: No orchestrator configured (try `ceph orch set backend`)

@sebastian-philipp
Copy link
Contributor

needs rebase

@sebastian-philipp
Copy link
Contributor

ping?

@liewegas
Copy link
Member Author

liewegas commented Apr 2, 2020

Rebased, but htis depends on #34272

@smithfarm
Copy link
Contributor

Just wondering, does this PR have any relation to https://tracker.ceph.com/issues/43114 ?

@liewegas
Copy link
Member Author

Just wondering, does this PR have any relation to https://tracker.ceph.com/issues/43114 ?

Not related. I think this is a useful cleanup/step forward for the CLI overall, but I was nervous about merging just prior to octopus. Now would be a good time to rebase and retest this, but hopefully someone can adopt it?

@liewegas
Copy link
Member Author

retest this please

Matthew Oliver and others added 7 commits April 17, 2020 17:45
This patch adds an new parameter to the argdesc class, `param` that when
set to `true` will force the `--name=` syntax in the generated helpstr.

The `param=true` is also added to the `fomat` option of the
`ceph orchestrator device ls` command so the specified usage wont result
in a cli syntax error.

This patch also modifies the ceph client to remove any param=true
argdesc's from the srgs and deals with them non positionally.

Fixes: https://tracker.ceph.com/issues/42113
Signed-off-by: Matthew Oliver <moliver@suse.com>
Signed-off-by: Sage Weil <sage@redhat.com>
Signed-off-by: Sage Weil <sage@redhat.com>
If the argdesc we are passed by the server has fields we do not recognize,
we currently choke with something like

Traceback (most recent call last):
  File "bin/ceph", line 1271, in <module>
    retval = main()
  File "bin/ceph", line 1050, in main
    return do_extended_help(parser, childargs, target, ' '.join(childargs))
  File "bin/ceph", line 416, in do_extended_help
    return help_for_target(target, partial)
  File "bin/ceph", line 413, in help_for_target
    return help_for_sigs(outbuf.decode('utf-8'), partial)
  File "bin/ceph", line 391, in help_for_sigs
    sys.stdout.write(format_help(parse_json_funcsigs(sigs, 'cli'),
  File "/home/sage/src/ceph3/src/pybind/ceph_argparse.py", line 847, in parse_json_funcsigs
    cmd['sig'] = parse_funcsig(cmd['sig'])
  File "/home/sage/src/ceph3/src/pybind/ceph_argparse.py", line 797, in parse_funcsig
    **kwargs))
  File "/home/sage/src/ceph3/src/pybind/ceph_argparse.py", line 673, in __init__
    self.instance = self.t(**self.typeargs)
TypeError: __init__() got an unexpected keyword argument 'positional'

At least, most types do: CephBool does not.

Make all of the types tolerate extra (new) fields.  This will make future
additions less painful.

Signed-off-by: Sage Weil <sage@redhat.com>
There are lots of CephBool parameters this applies too, but they already
look like non-positional arguments (as far as I can tell, at least).

There is *one* other command:

COMMAND("pg dump_stuck "
	"name=stuckops,type=CephChoices,strings=inactive|unclean|stale|undersized|degraded,n=N,req=false " \
	"name=threshold,type=CephInt,req=false",

In this case previously you could do 'pg dump_stuck inactive 123' and the
parser could tell 123 was not part of the stuckops list because it wasn't
in the enum.  With this PR you now have to do

 ceph pg dump_stuck inactive --threshold 123

IMO this is an improvement as it more closely aligns with how other
parsers work and is less ambiguous.

Signed-off-by: Sage Weil <sage@redhat.com>
Signed-off-by: Sage Weil <sage@redhat.com>
@stale
Copy link

stale bot commented Jun 17, 2020

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@stale stale bot added the stale label Jun 17, 2020
@stale
Copy link

stale bot commented Sep 20, 2020

This pull request has been automatically closed because there has been no activity for 90 days. Please feel free to reopen this pull request (or open a new one) if the proposed change is still appropriate. Thank you for your contribution!

@stale stale bot closed this Sep 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants