Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Cmd/Run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ const Subcmd RUN_CMD =
.setDesc("Build and execute src/main.cc")
.addOpt(OPT_RELEASE)
.addOpt(OPT_JOBS)
.setArg(Arg{ "args" }
.setDesc("Arguments passed to the program")
.setArg(Arg{ "[-- args]" }
Copy link
Member

Choose a reason for hiding this comment

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

Curious, is this the same as what Cargo prints? (I'm AFK)

Copy link
Author

Choose a reason for hiding this comment

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

It isn't what they print, but it is how they document it. https://doc.rust-lang.org/cargo/commands/cargo-run.html If you do cargo help run, you will see the same. But the actually cargo run --help is less clear.

Copy link
Member

Choose a reason for hiding this comment

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

I looked at the help and totally agree. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Did you check what the help looks like? Wouldn't it be like cabin run [OPTIONS] [[-- args]]...? I think we want something like cabin run [OPTIONS] [-- args...].

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.setArg(Arg{ "[-- args]" }
.setArg(Arg{ "-- args..." }

IIRC, you can change .setVariadic(true) to false and add a TODO for me.

.setDesc("Arguments passed to the program (use -- to "
"separate cabin options from program arguments)")
.setVariadic(true)
.setRequired(false))
.setMainFn(runMain);
Expand Down Expand Up @@ -61,6 +62,10 @@ static Result<void> runMain(const CliArgsView args) {
nextArg.data(), nextArg.data() + nextArg.size(), numThreads);
Ensure(ec == std::errc(), "invalid number of threads: {}", nextArg);
setParallelism(numThreads);
} else if (arg == "--") {
// End of cabin options, everything after is for the program
++itr;
break;
} else {
// Unknown argument is the start of the program arguments.
break;
Expand Down
38 changes: 38 additions & 0 deletions tests/06-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,42 @@ EOF
)
'

test_expect_success 'cabin run -- passes arguments to program' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$CABIN" new test_args &&
cd test_args &&
cp "$WHEREAMI/06-run/test_args.cc" src/main.cc &&
"$CABIN" run -- --help --version foo 1>stdout &&
(
cat >stdout_exp <<-EOF &&
argc=4
arg[1]=--help
arg[2]=--version
arg[3]=foo
EOF
test_cmp stdout_exp stdout
)
'

test_expect_success 'cabin run without -- stops at first unknown arg' '
OUT=$(mktemp -d) &&
test_when_finished "rm -rf $OUT" &&
cd $OUT &&
"$CABIN" new test_args2 &&
cd test_args2 &&
cp "$WHEREAMI/06-run/test_args.cc" src/main.cc &&
"$CABIN" run foo bar --release 1>stdout &&
(
cat >stdout_exp <<-EOF &&
argc=4
arg[1]=foo
arg[2]=bar
arg[3]=--release
EOF
test_cmp stdout_exp stdout
)
'

test_done
7 changes: 7 additions & 0 deletions tests/06-run/test_args.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <iostream>
int main(int argc, char* argv[]) {
std::cout << "argc=" << argc << '\n';
for (int i = 1; i < argc; ++i) {
std::cout << "arg[" << i << "]=" << argv[i] << '\n';
}
}
Loading