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

Passing args to a CLI app is not possible #70

Open
sharky98 opened this issue Dec 20, 2024 · 1 comment
Open

Passing args to a CLI app is not possible #70

sharky98 opened this issue Dec 20, 2024 · 1 comment

Comments

@sharky98
Copy link

I am creating a CLI app that have input arguments based on this example (I am learning Rust).

When I run nx run my_app:run --args="file1 file2", which is the typical Nx ways of forwarding arguments to the ran command. However, the buildCommand function push them as-is in-between other args destined for cargo run.

The resulting command built is cargo run --target-dir dist/target/my_app --args file1 file2 -p my_app, but cargo run --target-dir dist/target/my_app -p my_app -- file1 file2 should be the one expected.

Actual command with Nx VS expected cargo command

actual

nx run my_app:run --args="file1 file2"

> nx run my_app:run --args=file1 file2

> cargo run --target-dir dist/target/my_app --args file1 file2 -p my_app
error: unexpected argument '--args' found

  tip: a similar argument exists: '--target'
  tip: to pass '--args' as a value, use '-- --args'

Usage: cargo run --target-dir <DIRECTORY> --target [<TRIPLE>] [ARGS]...

For more information, try '--help'.

 NX   [object Object]



——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Ran target run for project my_app (631ms)

      With additional flags:
        --args=file1 file2

   ✖  1/1 failed
   ✔  0/1 succeeded [0 read from cache]

expected

cargo run --target-dir dist/target/my_app -p my_app -- file1 file2
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
     Running `dist/target/my_app/debug/my_app file1 file2`
model: "file1", output: "file2"

I think a test case could be written as below. Maybe the last entry "file1 file2", should be two elements, but I think Nx pass the whole thing as a string to the executor.

I'll see if I can submit a proper PR later on.

  it('user arguments (--args) should be put at the end after --', () => {
    const command = buildCommand(
      'run',
      { args: "file1 file2" },
      context
    );
    expect(command).toMatchInlineSnapshot(`
      Array [
        "run",
        "-p",
        "project",
        "--",
        "file1 file2",
      ]
    `);

And the buildCommand could be modified as such.

export function buildCommand(
  baseCommand: string,
  options: BaseOptions,
  context: ExecutorContext
): string[] {
  const args = [];

  if (options.toolchain && options.toolchain !== 'stable') {
    args.push(`+${options.toolchain}`);
  }

  args.push(baseCommand);

  for (const [key, value] of Object.entries(options)) {
    if (key === 'toolchain') {
      continue;
    }

+    if (key === 'args') {
+      continue;
+    }

    if (typeof value === 'boolean') {
      // false flags should not be added to the cargo args
      if (value) {
        args.push(`--${key}`);
      }
    } else if (Array.isArray(value)) {
      for (const item of value) {
        args.push(`--${key}`, item);
      }
    } else {
      args.push(`--${key}`, value);
    }
  }

  if (!args.includes("--package")) {
    args.push("-p", context.projectName);
  }

+  if (options.args) {
+    args.push("--", options.args);
+  }

  return args;
}
@sharky98
Copy link
Author

It appears there is already PR #42 for that! But I am not sure if it's the right approach using -- with the Nx run command, as their way of forwarding arguments is using --args as shown in their docs.

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

No branches or pull requests

1 participant