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

Surprising behaviour: argfile reads @argfiles after -- #46

Closed
passcod opened this issue Dec 10, 2023 · 2 comments
Closed

Surprising behaviour: argfile reads @argfiles after -- #46

passcod opened this issue Dec 10, 2023 · 2 comments

Comments

@passcod
Copy link

passcod commented Dec 10, 2023

Found in watchexec/watchexec#717.

When there's a trailing_var_arg (or in general, really), I would expect that argfile would not parse beyond the -- separator. That is, these should load an argfile:

command @argfile --option
command --option @argfile
command @argfile -- sub args

But this should not:

command --option -- @bazel-target-or-something

Here's a minimal repro:

#!/usr/bin/env cargo

//! ```cargo
//! [dependencies]
//! argfile = "0.1.6"
//! clap = { version = "4.4.11", features = ["derive"] }
//! ```

use clap::{Parser, ValueHint};

#[derive(Debug, Clone, Parser)]
pub struct Args {
	#[arg(
		trailing_var_arg = true,
		num_args = 1..,
		value_hint = ValueHint::CommandString,
		value_name = "COMMAND",
	)]
	pub command: Vec<String>,
}

fn main() {
    let args = argfile::expand_args(argfile::parse_fromfile, argfile::PREFIX)
		.expect("while reading @argfile");
    let args = Args::parse_from(args);
    dbg!(args);
}

Calling with ./repro.rs -- @not-an-argfile panics, while I would expect it to silently ignore and leave the @not-an-argfile in the args.

@epage
Copy link
Contributor

epage commented Dec 11, 2023

parse_fromfile is specifically imitating what Python does. Can you confirm if Python ignore it after an escape (--)?

@passcod
Copy link
Author

passcod commented Dec 11, 2023

Alright if you saw the previous version of this post, didn't realise you were talking about specifically the argparse module from Python; the answer is no, argparse doesn't ignore fromfiles after --.

#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument('--option')
parser.add_argument('--flag', action='store_true')
parser.add_argument('command', metavar='COMMAND', nargs='+')
print(parser.parse_args())
$ ./run.py -- foo
Namespace(option=None, flag=False, command=['foo'])

$ cat foo
--flag

$ ./run.py -- @foo
Namespace(option=None, flag=False, command=['--flag'])

@passcod passcod closed this as not planned Won't fix, can't repro, duplicate, stale Dec 11, 2023
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

2 participants