Get list of passed arguments in Windows batch script (.bat) – Stack Overflow

Argument parsing¶

For each command in the cmd2 subclass which requires argument parsing,
create a unique instance of argparse.ArgumentParser() which can parse the
input appropriately for the command.

Then decorate the command method with the
@with_argparser decorator, passing the argument parser as the first
parameter to the decorator. This changes the second argument to the command
method, which will contain the results of ArgumentParser.parse_args().

Here’s what it looks like:

importargparsefromcmd2importwith_argparserargparser=argparse.ArgumentParser()argparser.add_argument('-p','--piglatin',action='store_true',help='atinLay')argparser.add_argument('-s','--shout',action='store_true',help='N00B EMULATION MODE')argparser.add_argument('-r','--repeat',type=int,help='output [n] times')argparser.add_argument('word',nargs='?',help='word to say')@with_argparser(argparser)defdo_speak(self,opts)"""Repeats what you tell me to."""arg=opts.wordifopts.piglatin:arg='%s%say'%(arg[1:],arg[])ifopts.shout:arg=arg.upper()repetitions=opts.repeator1foriinrange(min(repetitions,self.maxrepeats)):self.poutput(arg)

Decorator order¶

If you are using custom decorators in combination with
@cmd2.with_argparser, then the
order of your custom decorator(s) relative to the cmd2 decorator matters
when it comes to runtime behavior and argparse errors.

There is nothing
cmd2-specific here, this is just a side-effect of how decorators work in
Python. To learn more about how decorators work, see decorator_primer.

If you want your custom decorator’s runtime behavior to occur in the case of
an argparse error, then that decorator needs to go after the
argparse one, e.g.:

However, if you do NOT want the custom decorator runtime behavior to occur
even in the case of an argparse error, then that decorator needs to go
before the arpgarse one, e.g.:

How to use cmd passed arguments in other function?

In order to make data available to other functions, you need to pass it as a parameter, or make it available through a global (not recommended) or a static variable.

static char** args cmd_args;
static int cmd_arg_count;

int main(int argc, char** argv) {
    cmd_arg_count = argc;
    cmd_args = argv;
    return 0;

void do_work() {
    if (cmd_args > 1) {
        printf("'%s'n", cmd_args[1]);

The best approach is to make a function that parses command line parameters, and stores the results in a struct that you define specifically for the purpose of representing command line arguments. You could then pass that structure around, or make it available statically or globally (again, using globals is almost universally a bad idea).

:/>  Как сделать скриншот (снимок) экрана в Windows 10

Passing arguments to cmd.exe

I know there are several post about passing arguments to a program but I haven’t been able to solve my problem.

I am trying to pass some arguments to a program from my c# code.

When adding the arguments /c < input.txt | program2 > output.txt I get an System.InvalidOperationException.

With no arguments passed, the code works fine and program1 starts.

I have not been able to figure out how to explain my arguments correct.

Possibly the use of <, >, | and the fact that there is a second program among the arguments gets me into trouble?

This works fine in cmd.

My code:

var myProcess = new System.Diagnostics.Process
    StartInfo = new System.Diagnostics.ProcessStartInfo
        FileName = "program1",
        Arguments = "/c < input.txt | program2 > output.txt",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = false

while (!myProcess.StandardOutput.EndOfStream)
    string line = myProcess.StandardOutput.ReadLine();

Passing command line arguments to gnuplot in windows

I have a Fortran 90 program that outputs some data to a .txt file.
The data is to be plotted with gnuplot.

I was able to launch gnuplot with

CALL SYSTEM("start wgnuplot")

which is equivalent to type

start gnuplot 

in the Windows command line prompt.

But then, I would like to have the program telling gnuplot what to do next, i.e., changing directory to the right one, and plotting my file.txt.

All in all this boils down to a simpler question:

How do I pass a command line in Windows that launches gnuplot and gives it some additional commands?

I tried to do that with something even easier like plotting y=x.
In a normal gnuplot windows this is just plot x.

From the cmd.exe (which is what is called by Fortran’s CALL SYSTEM() )I’ve tried:

start wgnuplot plot x
start wgnuplot plot x -pause
start wgnuplot plot x -persist
start wgnuplot plot x -noend
start wgnuplot plot x /noend

And others, including every possible variant with or without quotation marks, for instance

start wgnuplot "plot x -persist" 


So far the only one that works is the basic

start gnuplot

Which starts gnuplot indeed. But then I don’t know how to add the next commands. Once I have a working command line input I believe I will just have to plop it into the CALL SYSTEM argument to have my Fortran program doing all the work.

:/>  Применение твиков реестра ко всем учетным записям после установки ОС

I could only find instructions on how to achieve this on a UNIX-like machine, but not on Windows.
Any help would be appreciated.

Background info: Windows 8, Code::Blocks, gnuplot 5.0 patchlevel 1

Problem passing arguments with spaces to a process running under cmd.exe · issue #2835 · microsoft/wsl

I start with explaining what’s going on:

There are 4 translation/quoting steps involved in this scenario:

  1. /bin/bash translates the given command into the executable name and an array of arguments.
    (because on *nix new processes are started with an argument array)
    (used rules:
  2. WSL/lxss translates the array of arguments into a single string.
    (because on Windows, new processes are started with a single argument string)
    (used rules: AFAIK not documented, but it now correctly seems to be inverse to these rules)
  3. cmd.exe (a very old program…) does a weird string transformation, that breaks all usual behavior:
    Quote from Doc:

Processing quotation marks

If you specify /c or /k, cmd processes the remainder of String, and quotation marks are preserved only if all of the following conditions are met:

  • You do not use /s.
  • You use exactly one set of quotation marks.
  • You do not use any special characters within the quotation marks (for example: & < > ( ) @ ^ | ).
  • You use one or more white-space characters within the quotation marks.
  • The String within quotation marks is the name of an executable file.

If the previous conditions are not met, String is processed by examining the first character to verify whether it is an opening quotation mark. If the first character is an opening quotation mark, it is stripped along with the closing quotation mark. Any text following the closing quotation marks is preserved.

The German help-text is actually a little bit more clear: The phrase “closing quotation mark” is not the next quotation mark, but the last quotation mark of the string.

  1. args.exe translates the single argument string into an argument array.
    (because the C-Standard needs an argument array)
    (Rules depend on the compiler used to compile args.exe, however almost all compilers use these rules)
:/>  ARP Протокол Разрешения Адресов для чайников, как работает

Some notes:

Currently possible solution for your last example:

User@PC:~$ cmd.exe /c set "PATH=Z:bin;" "&" "args.exe" "foo bar"

Command-line arguments:
  argv[0]   args.exe
  argv[1]   foo bar

This works, because the first character, that cmd sees after /c is not a quotation mark, because the first argument, that WSL sees after /c does not contain a space.

Translation steps (I represent arrays as bullet-lists):
Original string:
/c set "PATH=Z:bin;" "&" "args.exe" "foo bar"
after step 1.:

  • /c
  • set
  • PATH=Z:bin;
  • &
  • args.exe
  • foo bar

after step 2.:
/c set PATH=Z:bin; & args.exe "foo bar"
because the first char after /c is not " cmd doesn’t remove any quotes and it works.

This is of course not a nice solution – but there is no really nice solution for windows executable that don’t follow typical rules.

I’d hope that Microsoft provides some wrapper, that just takes one argument, and uses that as complete commandline for Windows, for example:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
   if (argc!=2){
      printf("nPlease call with exactly one argument.n");

With such a tool it is possible to call programs like this:

/path/to/call.exe ‘commandline exactly as you would type in cmd or Win R-Dialog

Example output:

User@PC:~$ /mnt/d/call.exe 'cmd /c "z:binargs.exe "foo bar""'

Command-line arguments:
  argv[0]   z:binargs.exe
  argv[1]   foo bar
User@PC:~$ /mnt/d/call.exe 'cmd /c "set PATH=Z:bin; & args.exe "foo bar""'

Command-line arguments:
  argv[0]   args.exe
  argv[1]   foo bar

Refer all arguments passed at command line from a specific index to the end – batch script

Let’s say I’m passing some arguments from command line to a batch script.
Like: myfile.bat flower, tree, doge, cate, house
But the length of the argument list is dynamic, not 5 all the time.
How can I get all arguments from n to last.
For example, if n was 2, what should I write in order to get doge, cate, house.
Edit based on comment question.

Оставьте комментарий