Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A collection of command-line tools and utilities, streamlining wolframscript creation
ResourceFunction["CommandLineTools"]["Parse"] parses the command line creating an association of flag-argument pairs. | |
ResourceFunction["CommandLineTools"]["Parse",form] parses the command line and returns flag-argument pairs where the flag matches form. | |
ResourceFunction["CommandLineTools"]["Interface"] creates a blocking interface with default commands. | |
ResourceFunction["CommandLineTools"]["Interface",cmds] creates a blocking interface with additional custom commands cmds. | |
ResourceFunction["CommandLineTools"]["Log",lvl,msg] logs msg to stdout and to a file at log level lvl. | |
ResourceFunction["CommandLineTools"]["Log",lvl] is an operator form of Log method. | |
ResourceFunction["CommandLineTools"]["Help"] prints a help message to stdout. |
| "CommandLine" | Automatic | command line to parse |
| "POSIX" | False | restrict to the POSIX convention for short and long options |
| "InterfacePrompt" | "wls-cli>" | interface input prompt |
| "InterfaceCommands" | defaultCommands | delayed rules corresponding to the defined interface commands |
| "OptionDelimiter" | " " | "=" | option delimiter form |
| "ArgumentDelimiter" | "," | argument delimiter form |
| "HelpMessage" | "CommandLineTools example interface" | help message |
| "LogToFile" | True | whether "Log" should log to a file |
| "LogToStdOut" | True | whether "Log" should log to stdout |
| "LogDirectory" | "./logs/wls-cli" | directory log files should be placed in |
| "LogStyles" | defaultStyles | log type label style rules |
| "ArgumentSpecification" | None | option and argument pattern specification |
| "StrictParse" | False | return failure if unknown option is passed |
| "help" or "h" or "?" | print help message |
| "exit" or "quit" or "q" | exit the interface |
| "eval" or "e" | evaluate code in the session |
| n | boolean flag with long name n. |
| {n,s} | boolean flag long name n and short name s. |
| {n,s}→pat | option with long name n and short name s and pattern restriction pat on the argument. |
| {n,s}→pat→def | option with long name n and short name s with pattern restriction pat on the argument and default value def. |
Parse the $CommandLine:
| In[1]:= |
| Out[1]= |
Use FlagQ to quickly check if an argument is present in the command line, always returning True or False:
| In[2]:= | ![]() |
| Out[2]= |
Interface creates a blocking interface with predefined commands:
| In[3]:= |

Log is a convenient helper for logging to stdout and to a log file (or any combination of the two). Messages are formatted using ANSI codes which only work within terminal environments:
| In[4]:= |
| Out[4]= |
By default, the parser has no expected arguments and will accept almost any POSIX form with the exception of single-letter short flags:
| In[5]:= | ![]() |
| Out[6]= |
To avoid providing options each time CommandLineTools is called, the session defaults can be changed using SetOptions:
| In[7]:= | ![]() |
| Out[8]= |
Parse for a specified option:
| In[9]:= | ![]() |
| Out[9]= |
You can enable full POSIX parsing for single-character short options using the "POSIX" option:
| In[10]:= |
| Out[10]= |
Define an expected argument pattern and new test command:
| In[11]:= | ![]() |
| Out[12]= | ![]() |
By default, unknown arguments will be parsed as strings:
| In[13]:= |
| Out[13]= |
This behaviour can be changed by disabling the "StrictParse" option:
| In[14]:= | ![]() |
| Out[14]= |
FlagQ parses combined shorthand options if POSIX is set to True:
| In[15]:= | ![]() |
| Out[15]= |
FlagQ is around 10 to 25 times faster than the Parse method:
| In[16]:= | ![]() |
| Out[13]= |
More custom commands can also be defined using a second argument:
| In[17]:= | ![]() |

Log is a convenient helper for logging to stdout and to a log file (or any combination of the two). Messages are formatted using ANSI codes which only work within terminal environments:
| In[18]:= |
| Out[18]= |
The message and timestamp is also logged to log file, the location of which can be changed with an option:
| In[19]:= |
| Out[19]= |
Create a collection of loggers:
| In[20]:= | ![]() |
| Out[21]= |
Help automatically generates a help message based on your options and commands:
| In[22]:= | ![]() |
| Out[23]= | ![]() |
Define an example script deploying a local WL HTTP server using LocalDeploy:

Export the script:
| In[24]:= | ![]() |
| Out[24]= |
Get help on how to run the script:

Run the script:

Evaluate an expression in the session:

Make a request to the server:
| In[25]:= |
| Out[25]= | ![]() |
Exit the script using the quit command:

Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License