Skip to content

Conversation

@casperdcl
Copy link
Contributor

@casperdcl casperdcl commented Jun 21, 2020

- makes help output look the same as before iterative#3921
@casperdcl casperdcl requested review from efiop and shcheklein June 21, 2020 00:46
@casperdcl casperdcl changed the title Completion misc completion: misc fixes Jun 21, 2020
@casperdcl casperdcl self-assigned this Jun 21, 2020
@casperdcl casperdcl added bug Did we break something? completion enhancement Enhances DVC ui user interface / interaction labels Jun 21, 2020
@casperdcl casperdcl mentioned this pull request Jun 21, 2020
5 tasks
@casperdcl casperdcl added this to the 1.0 milestone Jun 21, 2020
Copy link
Contributor

@efiop efiop left a comment

Choose a reason for hiding this comment

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

So a hack for now 🙁 Is there a possibility to make shtab not affect this in the future?

@efiop efiop merged commit 061513a into iterative:master Jun 21, 2020
@casperdcl
Copy link
Contributor Author

casperdcl commented Jun 22, 2020

Not a hack? Argparse always adds [] around options and {} around choices, and uses dest as the display name. metavar is the mechanism provided for the purpose of overriding all of this, and was used a lot in the codebase already.

The 29 places where choices were added in #3921 also needed metavar (added here in #4076) to control display.

There are 2 options:

  1. make shtab infer completion type (e.g. DVC_FILE) based on dest (e.g. target)

    • decided against this as it forces devs to be consistent with their own naming conventions
  2. explicitly mark completion types
    a) add_argument(dest, choices=[shtab.<TYPE>], metavar=dest)

    • current approach
    • dropping metavar adds {} around the display name. The display name itself is easily controllable via shtab.<TYPE>.__str__.
    • shtab.<TYPE> compares True to everything

    b) add_argument(shtab.Complete(dest, "<TYPE>"))

    • shtab.Complete inherits from str

I prefer (1) followed by (2a) and then (2b), but let me know your thoughts

cc @iterative/engineering

@efiop
Copy link
Contributor

efiop commented Jun 22, 2020

@casperdcl I mean that this metavar overwrite is only needed because we use shtab, we didn't use metavars before. Ideally, shtab shouldn't require the users to use such hacks, but at the same time this not that bad, just need to document it. Maybe it could even be used to our advantage if we could make the auto-generated metavar look better, but I'm not even sure it is possible.

@casperdcl
Copy link
Contributor Author

When you say "auto-generated metavar" what do you mean?

  1. what's displayed with add_argument("target", choices=[DVC_FILE])
    • currently {DVCFile}, could be {any-thing|*.dvc|can't avoid {} though}
  2. what's displayed with add_argument("target", choices=[DVC_FILE], metavar="target")
    • target
  3. what could be displayed if we subclass & override the add_argument method to auto-fill metavar?

@efiop
Copy link
Contributor

efiop commented Jun 22, 2020

@casperdcl I mean add_argument("target"), that would result in target. But when we start using shtab, we get {DVCFile...} or url [{directory?}](which is absolutely confusing), which is a side-effect. Just wondering if we could avoid the side-effect or, possibly, embrace it and try to make it look nice and be helpful. Maybe the latter could be achieved by making shtab choices have their own nice-looking metavars, not sure.

@casperdcl
Copy link
Contributor Author

casperdcl commented Jun 22, 2020

I mean add_argument("target"), that would result in target.

yes

But when we start using shtab, we get {DVCFile...}

That never happens (previously or currently).

Instead, with add_argument("target", choices=[DVC_FILE], ...), we used to get {DVCFile}, [{DVCFile}], or [{DVCFile} [{DVCFile} ...]] for required, optional, and optional list, respectively.

Now, with add_argument("target", choices=[DVC_FILE], metavar="DVCFile"), we get DVCFile, [DVCFile], or [DVCFile [DVCFile ...]], respectively, thus restoring the original look (without the {}).

or [{directory?}]

This was the case with add_argument("--optional", choices=[OPTIONAL_DIR]).

Now, with add_argument("--optional", choices=[OPTIONAL_DIR], metavar="directory"), we get [directory], restoring the original look (without {}). It also suppresses shtab's ? feature which arguably could be removed (there's no need for shtab to expressly mark optionals when argparse already does so by adding []).

The options we have are:

  1. previous: use choices to specify type, accepting the extra {}
  2. current: use choices to specify type and metavar to suppress {}
  3. use add_argument(ShtabString("target", type=DVC_FILE))

In all proposed options, we have trivial full control over outputting DVCFile and ? (i.e. it takes one line of code to globally change [{DVCFile?}] to [{DVCFile}] or [{*.dvc|dvc.yml}]).

which is a side-effect.

There are 3 side-effects so I don't know which you refer to:

  1. added {}
  2. added ?
  3. rename target -> DVCFile

Side-effects (2) and (3) are trivially globally configurable by one line of code, side-effect (1) is a built-in argparse feature if using choices.

@Suor
Copy link
Contributor

Suor commented Jun 23, 2020

So the question is - can we make shtab to not use choices?

@casperdcl
Copy link
Contributor Author

casperdcl commented Jun 23, 2020

So the question is - can we make shtab to not use choices?

The only way I can see is the third option above with:

add_argument(ShtabString("target", type=DVC_FILE))

where ShtabString is just a str with an extra type parameter

@casperdcl
Copy link
Contributor Author

casperdcl commented Jul 1, 2020

For the record: #4148 uses add_argument("target").complete = DVC_FILE

@casperdcl casperdcl mentioned this pull request Jul 1, 2020
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Did we break something? enhancement Enhances DVC ui user interface / interaction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants