Former question: How to use wolframscript to develop flexible command-line tools?
I am currently changing a notebook into a wolframscript for a "production" environment, where I plan to pack the script as a standard CLI tool. However I do not know how to implement such an interface, where boolean, switch-like, argument accepting and other sorts of options are supported.
In bash
python
and many other scripting languages, either the parsing functionality is built-in,
or there exists (semi-)official parsing modules for this purpose, named getopt
argparse
or similar.
As of my knowledge and of Jan 2021, there is no such libraries for wolframscript yet. The GetOpts
package by @McSaks, a candidate for this, has been out of maintenance for years, and no longer suffice modern needs.
There does exist a -args
option in the grammar, supplied to the executed command,
after the main -c|-f|-api|-fun
parameter for the interpreter
(which did not seem to exist when the related questions on SE were asked).
But it is de facto not documented, thus leaving me confused.
Related resources
Based on the answer by @Mr Puh, I think the wording of my question may not be clear enough, and the following resources may be helpful for discussion:
- C library function
getopt
- Wikipedia - C++
Boost.Program_options
- Built-in Unix shell command
getopts
- Wikipedia - Python 3 standard library |
getopt
— C-style parser for command line options (not recommended) - Python 3 standard library |
argparse
— Parser for command-line options, arguments and sub-commands - Python 3 HOWTOs | Argparse Tutorial
- Google Commandline Flags
- C++ 3rd party library
argparse
by @p-ranav - Haskell command line option parsers
Related questions (sorted by relevance)
- Passing arguments into a script in WolframScript
- Creating flags to use with wolframscript?
- Parse command line arguments
- Mathematica script - passing command line arguments
Since this thread is not receiving extensive replies, I now wonder what is the best practice of wolframscript
. What's the use intended by WRI? What's the most common official practice?
Put[APIFunction[...], LocalObject["yourscriptname"]]
and thenwolframscript -api yourscriptname -local -args x=1 y=string z='10 meters'
. $\endgroup$LocalObject
as a API? We know that aLocalObject
is a directory in the$Path
, with aobject.wl
file and additional files with definitions. With the code you provided, there will be aput.wl
which contains a singalAPIFunction
expression. $\endgroup$