How to get parameters of script in subroutine

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sincos2007
Posts: 44
Joined: 05 Apr 2019 05:52

How to get parameters of script in subroutine

#1 Post by sincos2007 » 03 Jun 2019 06:34

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to get parameters of script in subroutine

#2 Post by dbenham » 03 Jun 2019 07:33

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

Post Reply