Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Space-separated subcommands #51

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

borekb
Copy link

@borekb borekb commented Nov 14, 2018

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: oclif/config#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 ID user:add and ['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 splitArgv function should be unit-tested. I didn't find a testing infrastructure in this package, will seek advice on how to best do it.

Related:

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 ID `user:add` and `['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 `splitArgv` function should be unit-tested. I didn't find a testing infrastructure in this package, will seek advice on how to best do it.
@salesforce-cla
Copy link

Thanks for the contribution! Before we can merge this, we need @borekb to sign the Salesforce.com Contributor License Agreement.

@codecov
Copy link

codecov bot commented Nov 14, 2018

Codecov Report

Merging #51 into master will increase coverage by 2.7%.
The diff coverage is 100%.

Impacted file tree graph

@@            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%) ⬆️
src/util.ts 93.75% <100%> (ø)
src/index.ts 77.27% <0%> (-0.51%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 46456dd...42179f4. Read the comment docs.

@borekb
Copy link
Author

borekb commented Nov 14, 2018

Sorry, total blindness, tests are obviously there :)

src/main.ts Outdated Show resolved Hide resolved
@borekb
Copy link
Author

borekb commented Nov 15, 2018

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 42179f4.

@lots0logs
Copy link

@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? 🤞

@borekb
Copy link
Author

borekb commented Feb 11, 2019

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.

@tcf909
Copy link

tcf909 commented Jul 19, 2019

@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.

@borekb
Copy link
Author

borekb commented Jul 22, 2019

@tcf909 Hi, we moved off of oclif so I won't be able to help.

@G-Rath
Copy link

G-Rath commented Nov 8, 2019

@oclif @heroku-cli I'm happy to champion this PR if you're willing to review it :)

@RasPhilCo
Copy link
Contributor

RasPhilCo commented Nov 12, 2019

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants