How to Pass Command Line Parameters from a File

How do I use the lines of a file as arguments of a command?

If your shell is bash (amongst others), a shortcut for $(cat afile) is $(< afile), so you'd write:

mycommand "$(< file.txt)"

Documented in the bash man page in the 'Command Substitution' section.

Alterately, have your command read from stdin, so: mycommand < file.txt

How to pass command line parameters from a file

With most shells, you can insert the contents of a file into a command line with $(<filename):

./myprogram $(<arguments.dat)

If your shell doesn't support that, then one of the older ways will work:

./myprogram $(cat arguments.dat)
./myprogram `cat arguments.dat` # need this one with csh/tcsh

(You do know the difference between command line arguments and file input, right? Why would you expect to pipe command line arguments into a program?)

Best way to pass command line arguments via file in python

If you plan to use argparse, then fromfile_prefix_chars is designed to solve exactly this problem.

In your launching program, put all of the arguments, one per line, into a file. Pass @file.txt to your child program. In your child program, pass a fromfile_prefix_chars parameter to the ArgumentParser() constructor:

parser = argparse.ArgumentParser(fromfile_prefix_chars='@')

argparse takes care of the rest for you.

Here is an example:

from argparse import ArgumentParser

parser = ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument('-f', '--foo')
parser.add_argument('--bar')
parser.add_argument('q', nargs='*')

ns = parser.parse_args()
print(ns)

The contents of foo.txt:

-f
1
--bar=2
q one
q two

The command line and the output:

$ python zz.py @foo.txt
Namespace(bar='2', foo='1', q=['q one', 'q two'])

How to read command line arguments from a text file?

If the reading of the parameters must be done solely from the file having it's name, the idiomatic way is, I would say, to use getline().

std::ifstream ifs("text.txt");
if (!ifs)
std::cerr << "couldn't open text.txt for reading\n";
std::string line;
std::getline(ifs, line);
int integer = std::stoi(line);
std::getline(ifs, line);
std::string string1 = line;
std::getline(ifs, line);
std::string string2 = line;

Because there are little lines in your file, we can allow ourselves some repetition. But as it becomes larger, you might need to read them into a vector:

std::vector<std::string> arguments;
std::string line;
while (getline(ifs, line))
arguments.push_back(line);

There some optimizations possible, as reusing the line buffer in the first example and using std::move(), but they are omitted for clarity.

How to pass the contents of a file as a parameter on the command line?

Assuming fizz contains a single line with no whitespace, the one-liner

for /F %i in (fizz) do @foo %i

will do what you want. If the argument you're putting into fizz may contain whitespace, use

 for /F "delims=" %i in (fizz) do @foo "%i"

to pass the argument in one piece. Double the % characters if putting this into a batch file.

Command line arguments, reading a file

You can use int main(int argc, char **argv) as your main function.

argc - will be the count of input arguments to your program.

argv - will be a pointer to all the input arguments.

So, if you entered C:\myprogram myfile.txt to run your program:

  • argc will be 2
  • argv[0] will be myprogram.
  • argv[1] will be myfile.txt.

More details can be found here

To read the file:

FILE *f = fopen(argv[1], "r"); // "r" for read

For opening the file in other modes, read this.

How can I pass arguments to a batch file?

Here's how I did it:

@fake-command /u %1 /p %2

Here's what the command looks like:

test.cmd admin P@55w0rd > test-log.txt

The %1 applies to the first parameter the %2 (and here's the tricky part) applies to the second. You can have up to 9 parameters passed in this way.



Related Topics



Leave a reply



Submit