TransWikia.com

How to create a CLI for Wolfram language scripts?

Mathematica Asked by Gravifer on February 23, 2021

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:

  1. C library function getopt – Wikipedia
  2. C++ Boost.Program_options
  3. Built-in Unix shell command getopts – Wikipedia
  4. Python 3 standard library | getopt — C-style parser for command line options (not recommended)
  5. Python 3 standard library | argparse — Parser for command-line options, arguments and sub-commands
  6. Python 3 HOWTOs | Argparse Tutorial
  7. Google Commandline Flags
  8. C++ 3rd party library argparse by @p-ranav
  9. Haskell command line option parsers

Related questions (sorted by relevance)

  1. Passing arguments into a script in WolframScript
  2. Creating flags to use with wolframscript?
  3. Parse command line arguments
  4. 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?

One Answer

As there are no answers yet, I'd like to share how I use wolframscript from the command line, please correct me if I am wrong.

Here are two possibilities I know of:

Converting a notebook to a script

  1. You create a notebook (.nb), convert all the cells relevant to initialization cells and save it as an .m file.

  2. You can use wolframscript -script scriptname arg1 arg2 arg3 now and arg1 will be available as within the script as $ScriptCommandLine[[2]]]. Also check this documentation.

Use python

There is a wolframlanguage plugin for python now. You can use this to call functions in the namespace of one of your wolfram packages. So first write a package, then in python do something like this:

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl

with WolframLanguageSession() as s:

    # Locate the mathematica package to install
    s.evaluate(wl.SetDirectory(s.evaluate(wl.Directory())+path_to_mathematica_m))
    s.evaluate(wl.Import("YOUR_PACKAGE.m"))
    
    # This calls the wolfram language function with arguments
    output_expression = wl.YOUR_PACKAGE.Function1(an_array.tolist(),arg2,arg3)

    output = s.evaluate(output_expression)

    # Then do something with the output. E.g. Export it to a file:
    s.evaluate(wl.Export(exportpath,output))

This allows you to use the wolframlanguage via python and thus you can use all the tools you have for python also for the wolframlanguage.

Answered by Mr Puh on February 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP