Repository: oclif/core
Pull request #203 (2021-07-14 16:58:09)

fix: respect variable args when using spaces
@W-9546117@


Patch:
diff --git a/src/config/config.ts b/src/config/config.ts
index 9cd2ef7..aa2d3f0 100644
--- a/src/config/config.ts
+++ b/src/config/config.ts
@@ -536,6 +536,7 @@ export async function toCached(c: Command.Class, plugin?: IPlugin): Promise<Comm
     id: c.id,
     summary: c.summary,
     description: c.description,
+    strict: c.strict,
     usage: c.usage,
     pluginName: plugin && plugin.name,
     pluginType: plugin && plugin.type,
diff --git a/src/help/util.ts b/src/help/util.ts
index d5454f9..72fbb40 100644
--- a/src/help/util.ts
+++ b/src/help/util.ts
@@ -48,7 +48,7 @@ function collateSpacedCmdIDFromArgs(argv: string[], config: IConfig): string[] {
 
     const hasSubCommandsWithArgs = () => {
       const subCommands = config.commands.filter(c => (c.id).startsWith(finalizeId()))
-      return Boolean(subCommands.find(cmd => cmd.args.length > 0))
+      return Boolean(subCommands.find(cmd => !cmd.strict || cmd.args.length > 0))
     }
 
     for (const arg of argv) {
diff --git a/src/interfaces/command.ts b/src/interfaces/command.ts
index db6dc32..e13e0cf 100644
--- a/src/interfaces/command.ts
+++ b/src/interfaces/command.ts
@@ -57,6 +57,7 @@ export interface Command extends CommandProps {
   pluginType?: string;
   flags: {[name: string]: Command.Flag};
   args: Command.Arg[];
+  strict: boolean;
 }
 
 export namespace Command {
@@ -89,6 +90,7 @@ export namespace Command {
     plugin?: Plugin;
     flags?: FlagInput<any>;
     args?: ArgInput;
+    strict: boolean;
     new(argv: string[], config: Config): Instance;
     run(argv?: string[], config?: LoadOptions): PromiseLike<any>;
   }


Test patch:
diff --git a/test/help/util.test.ts b/test/help/util.test.ts
index 0454d96..7c1e791 100644
--- a/test/help/util.test.ts
+++ b/test/help/util.test.ts
@@ -65,5 +65,42 @@ describe('util', () => {
       const actual = standardizeIDFromArgv(['foo', 'ba', '--baz'], config)
       expect(actual).to.deep.equal(['foo:ba', '--baz'])
     })
+
+    test
+    .stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
+    .it('should return standardized id when topic separator is a space and has args', () => {
+      config.topicSeparator = ' '
+      config.commands.push({
+        id: 'foo:bar',
+        args: [{name: 'first'}],
+      } as any)
+      const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
+      expect(actual).to.deep.equal(['foo:bar', 'baz'])
+    })
+
+    test
+    .stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
+    .it('should return standardized id when topic separator is a space and has variable arguments', () => {
+      config.topicSeparator = ' '
+      config.commands.push({
+        id: 'foo:bar',
+        strict: false,
+      } as any)
+      const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
+      expect(actual).to.deep.equal(['foo:bar', 'baz'])
+    })
+
+    test
+    .stub(Config.prototype, 'commandIDs', () => ['foo', 'foo:bar'])
+    .it('should return full id when topic separator is a space and does not have arguments', () => {
+      config.topicSeparator = ' '
+      config.commands.push({
+        id: 'foo:bar',
+        args: [],
+        strict: true,
+      } as any)
+      const actual = standardizeIDFromArgv(['foo', 'bar', 'baz'], config)
+      expect(actual).to.deep.equal(['foo:bar:baz'])
+    })
   })
 })