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"]["FlagQ",form] returns True or False if flag matching form is passd to the command line. | |
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 |
| "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 $CommandLine:
| In[1]:= |
| Out[1]= |
By default, the parser has no predefined argument patterns and will accept almost any POSIX form with the exception of single-letter short flags:
| In[2]:= | ![]() |
| Out[3]= |
Change the session defaults using SetOptions:
| In[4]:= | ![]() |
| Out[5]= |
Parse for a specific option:
| In[6]:= | ![]() |
| Out[6]= |
You can enable full POSIX parsing for single-character short options using the "POSIX" option:
| In[7]:= |
| Out[7]= |
Define an expected argument pattern and new test command:
| In[8]:= | ![]() |
Parse the command line:
| In[9]:= |
| Out[9]= |
| In[10]:= | ![]() |
| Out[11]= | ![]() |
By default, any unknown arguments will be parsed as strings:
| In[12]:= |
| Out[12]= |
This behaviour can be changed by disabling the "StrictParse" option:
| In[13]:= | ![]() |
| Out[13]= |
Use FlagQ to quickly check if an argument is present in the command line:
| In[14]:= | ![]() |
| Out[14]= |
Note that this doesn't work with combined, pre-defined shorthand options:
| In[15]:= | ![]() |
| Out[15]= |
FlagQ is less versatile than Parse but is around 10x faster (with caching on parse):
| In[16]:= | ![]() |
| Out[17]= |
FlagQ does not support the combined POSIX shorthand convention:
| In[18]:= | ![]() |
| Out[18]= |
Interface can be used to create an interactive, blocking dialog with a script with predefined commands:
| In[19]:= |

More custom commands can also be defined using a second argument:
| In[20]:= | ![]() |

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[21]:= |
| Out[21]= |
The message and timestamp is also logged to log file, the location of which can be changed with an option:
| In[22]:= |
| Out[22]= |
Create a collection of loggers:
| In[23]:= | ![]() |
| Out[24]= |
Help automatically generates a help message based on your options and commands:
| In[25]:= | ![]() |
| In[26]:= | ![]() |
Define an example script deploying a local WL HTTP server using LocalDeploy:

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

Run the script:

Evaluate an expression in the session:

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

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