Page 1 of 1

How to get parameters of script in subroutine

Posted: 03 Jun 2019 06:34
by sincos2007
command line:

Code: Select all

abc.bat -i valOfI -o valOfO
In abc.bat, define a subroutine func1. In func1, get valOfI and valOfO from command line. Order of “-i valOfI” and “-o valOfO” can be any in command line.

Thanks

Re: How to get parameters of script in subroutine

Posted: 03 Jun 2019 07:33
by dbenham
Batch does not have any built in functionality for named arguments. You will need batch code to parse named arguments and store the values in environment variables. Once you have that, you can do anything you want with the values, including pass them to called functions. Things to consider:
- How to handle invalid options
- What are default values if named arguments are not provided
- Do you also have or want unnamed (positional) arguments?

Have a look at my StackOverflow answer to a question about Windows Bat file optional argument parsing. I frequently use some variant of that technique in many of my own scripts to great effect.

HexDump.bat
HashSum.bat
JSort.bat
JRepl.bat
JTimeStamp.bat


Dave Benham