Space-separated subcommands
This PR adds support for space-separated subcommands at the @oclif/command level. The support also needs to be added in @oclif/config or possibly via plugin, I'm not sure yet – I'll open a PR in @oclif/config to show one way to do it (UPDATE: PR created: https://github.com/oclif/config/pull/64).
The change is rather small and backwards-compatible with colon-separated topics, here is a copy of the commit description:
Previously, Main.run considered the first item in argv array a command ID, for example, in the ['user:add', 'Peter'] array, the command was user:add and the rest were its args. This doesn't work with space-separated commands which produce argv array like this:
['user', 'add', 'Peter']
and would be parsed as a user command that gets add and Peter as args.
The new implementation looks at config.commandIDs and tries to find the longest match with items from argv. For example, if commandIDs contained user and user add commands, the new implementation would invoke a user add command with ['Peter'] as args. If, on the other hand, commandIds only contained the user command, it would invoke a user command with ['add', 'Peter'] as args.
A couple of notes:
- It's up to @oclif/config (or possibly a plugin) to produce command IDs that contain spaces. As of @oclif/[email protected], the command IDs contain a colon, e.g.,
user:add. - The change is backwards compatible. Argv
['user:add', 'Peter']is still parsed as command IDuser:addand['Peter']as args. - The command separator is currently hardcoded to be a space. If oclif introduced a new config option like
"separator": "<any_character>", the argv splitting logic should be updated. - ~~The
splitArgvfunction should be unit-tested. I didn't find a testing infrastructure in this package, will seek advice on how to best do it.~~
Related:
- Support for space-separated subcommands https://github.com/oclif/oclif/issues/186
Thanks for the contribution! Before we can merge this, we need @borekb to sign the Salesforce.com Contributor License Agreement.
Codecov Report
Merging #51 into master will increase coverage by
2.7%. The diff coverage is100%.
@@ Coverage Diff @@
## master #51 +/- ##
=========================================
+ Coverage 70.37% 73.07% +2.7%
=========================================
Files 4 5 +1
Lines 135 156 +21
Branches 28 32 +4
=========================================
+ Hits 95 114 +19
- Misses 24 26 +2
Partials 16 16
| Impacted Files | Coverage Δ | |
|---|---|---|
| src/main.ts | 62.96% <100%> (+1.42%) |
:arrow_up: |
| src/util.ts | 93.75% <100%> (ø) |
|
| src/index.ts | 77.27% <0%> (-0.51%) |
:arrow_down: |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update 46456dd...42179f4. Read the comment docs.
Sorry, total blindness, tests are obviously there :)
Pushed some improvements but also realized that the algo is not entirely correct, for example, command IDs ['user add2'] should not work with argv ['user', 'add'] (note the missing "2") but it does. Will work on it further.
Update: this has been fixed by https://github.com/oclif/command/pull/51/commits/42179f45a462a7915faf3a4ce5ce3c811feb0e3b.
@borekb I'm very interested in seeing your PRs merged. Could you rebase on latest master so that they are ready whenever the maintainers are ready to (hopefully) approve them? :crossed_fingers:
Hi @lots0logs, I don't think there's a big desire by the team to support this. If they change their minds, I'll be more than happy to help with PRs.
@borekb Curious if you are using this at all in the wild? How hardened is the code? Would it be worth it to maintain a separate fork if the project owners aren't willing to support the efforts? I noticed Adobe open-sourced a cli based on oclif and they included space separated commands, but unfortunately their implementation isn't complete.
All this to say I'd be happy to help maintain a separate fork if your interested in some help.
@tcf909 Hi, we moved off of oclif so I won't be able to help.
@oclif @heroku-cli I'm happy to champion this PR if you're willing to review it :)
@G-Rath Plugins would have to support this too, as help and autocomplete are currently incompatible. I like this feature and want it (I wrote a hack to implement it), but we're prioritizing some help templating work first.