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.
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:
getopt
– WikipediaBoost.Program_options
getopts
– Wikipediagetopt
— C-style parser for command line options (not recommended)argparse
— Parser for command-line options, arguments and sub-commandsargparse
by @p-ranavSince 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?
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:
You create a notebook (.nb), convert all the cells relevant to initialization cells and save it as an .m file.
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.
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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP