Function Repository Resource:

CommandLineParse (1.0.0) current version: 1.1.0 »

Source Notebook

Parse a command, returning a Dataset of all flags present command and their associated parameter values

Contributed by: Antonis Aristeidou

ResourceFunction["CommandLineParse"][]

parses flags and parameters in $CommandLine and returns an associated Dataset of parameters and values.

ResourceFunction["CommandLineParse"]["cmd"]

parses flags and parameters in the command string "cmd".

ResourceFunction["CommandLineParse"]["-flag"]

returns parameters associated with flag in $CommandLine.

ResourceFunction["CommandLineParse"]["cmd", "-flag"]

returns parameters associated with flag in "cmd".

Details

The "cmd" can be a command string or a list of the form {"command", "flag1", "param2", …}.
The no command form of ResourceFunction["CommandLineParse"] will try to parse $CommandLine.

Examples

Basic Examples

Parse the current session's $CommandLine:

In[1]:=
ResourceFunction["CommandLineParse"][]
Out[1]=

Parse a command string:

In[2]:=
ResourceFunction["CommandLineParse", ResourceVersion->"1.0.0"]["WolframKernel -wstp -noicon -linkprotocol SharedMemory -linkmode connect -linkname iwb2p_shm"]
Out[2]=

Parse a command list:

In[3]:=
ResourceFunction["CommandLineParse"][{"WolframKernel",
  "-wstp",
  "-noicon",
  "-linkprotocol", "SharedMemory",
  "-linkmode", "connect",
  "-linkname", "iwb2p_shm"
  }]
Out[3]=

Parse a compound command:

In[4]:=
cmds = ResourceFunction["CommandLineParse"][
  "git pull --branch branch/name -r args && git submodule update -r"]
Out[4]=
In[5]:=
cmds["git pull", Select[#Flag == "--branch" &], "Parameter"]
Out[5]=

Parse $CommandLine returning the parameters associated to the "linkmode" flag:

In[6]:=
ResourceFunction["CommandLineParse"]["-linkmode"]
Out[6]=

Parse a command list for multiple flags:

In[7]:=
ResourceFunction["CommandLineParse"][
 {
  "WolframKernel",
  "-wstp",
  "-noicon",
  "-linkprotocol", "SharedMemory",
  "-linkmode", "connect",
  "-linkname", "iwb2p_shm",
  "-flag", "arg1", "arg2"
  },
 {
  "-flag",
  "-linkname",
  "-linkmode"
  }
 ]
Out[7]=

Parse a compound command for a flag:

In[8]:=
ResourceFunction["CommandLineParse", ResourceVersion->"1.0.0"]["git pull --branch branch/name -r argument && git submodule update -r && exit", "-r"]
Out[8]=

A Failure object is returned if the flag is not found:

In[9]:=
ResourceFunction["CommandLineParse"]["-unknown"]
Out[9]=
In[10]:=
ResourceFunction["CommandLineParse", ResourceVersion->"1.0.0"]["git pull --branch branch/name && git submodule update && exit ", "-unknown"]
Out[10]=

$Failed is returned if an unknown input is received:

In[11]:=
ResourceFunction["CommandLineParse"][6]
Out[11]=
In[12]:=
ResourceFunction["CommandLineParse", ResourceVersion->"1.0.0"][$CommandLine, "-flag", extraArgument]
Out[12]=

Publisher

Tony Aristeidou

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.1.0 – 09 August 2024
  • 1.0.0 – 19 July 2024

Related Resources

License Information