[  Up Level |  Project home |  Index |  Class hierarchy ]

 menu.ox

Keyboard and Command Line Menus for Running Functions and Setting Parameter Values; Get the Latest Release.

This package offers two classes for prompting user of an Ox program to select from a menu.
CallMenu handles options to call different Ox functions, including submenus
ParamMenu handles a list of parameters to send to a function

The programmer creates an object of the class for the menu. It then adds a list of items for the menu.
CallMenu an item is array of two things: prompt and either a functions to run or submenu to enter.
For ParamMenu an item is a prompt and a default value of the parameter.

The program can run a menu interactively or read options from command line arguments.

Interactive Mode

CallMenu prints the menu and reads integer selections from the keyboard. It adds options to run all the items on the list and to navigate submenus, exit Ox altogether or simply quit the menu and return to the program.
ParamMenu prints the parameter list and reads integer selections of values to change. A second prompt then reads new values from the keyboard. Matrix elements are entered by row and must match the dimension of the default value. An option to reset all parameters. Another option either returns parameters as a list to the calling program or sends the list to a function sent to the interactive menu. If a function is called, the program returns to the menu and the user can enter new values and continue to rerun the function.

Command Line Mode
The command line mode reads arguments sent to the program past a point specified (the default is all command line arguments or menu options.
For CallMenu the items are read as if entered from the keyboard including the DOALL option. Once a function has been called the menu automatically returns to the top menu and starts over. (In interactive mode the user enters a value to move up a level.)
For ParamMenu if no arguments are given then default parameters are returned or used. Otherwise command line arguments are in the form: k=val where k is the index, "=" is the equal sign and val is either an integer, double or a string containing elements of a matrix parameter to be scanned in using the "%M" formatting code.

An CallMenu example: examples/example1.ox.

Code

Output


A ParamMenu example:examples/example2.ox.

Code


Output

    
Author:
Copyright © 2017-2020 Christopher Ferrall.

 CallMenu : Menu

A class for choosing from a menu of functions and submenus interactively or from the command line.
Public fields
 help_text string printed if HELP selected.
 IamMain TRUE if top menu.
Public methods
 add Add an item to the Callmenu.
 CallMenu Create a new CallMenu object.
 CmdLine Run options from command line if any, otherwise go interactive.
 make_the_call Call the function or enter the submenu selected.
 Run Run the menu.
Inherited methods from Menu:
itemparse, Menu, SetLogDir, ~Menu
Inherited fields from Menu:
items, keeplog, logdir, name, sep
Inherited enumerations from Menu:
Anonymous enum 1, Anonymous enum 2

 Menu

A menu object.
Public fields
 items array containing menu.
 keeplog log calls from this menu.
 logdir static Folder for logs.
 name string: menu name.
 sep static const
Public methods
 add virtual
 CmdLine virtual
 itemparse static Find the menu item corresponding to the token.
 Menu Create a new menu object.
 SetLogDir static Set the static folder to place logs of calls for all menus.
 ~Menu
Enumerations
 Anonymous enum 1
 Anonymous enum 2

 ParamMenu : Menu

A class for reading in parameters interactively.
Public fields
 pars
Public methods
 add add parameters to the list.
 CmdLine Set parameter values from the command line.
 ParamMenu
 SetPars Set Parameters.
Enumerations
 Anonymous enum 1
Inherited methods from Menu:
itemparse, Menu, SetLogDir, ~Menu
Inherited fields from Menu:
items, keeplog, logdir, name, sep
Inherited enumerations from Menu:
Anonymous enum 2

 CallMenu

 add

CallMenu :: add ( ... )
Add an item to the Callmenu.
Parameters:
... list of items, each an array of size ItemSize

The first element of an item is a prompt string. The second is either a static function of the form func(). Or, it is another menu object that is a submenu. One way to organize submenus is to write a function that creates and returns the submenu object then call it and return its value as the call item as shown below.

Example:
m->add( {"Call 1",call1}, {"Call 2",call2}, {"Sub ",menu3()} );

 CallMenu

CallMenu :: CallMenu ( name , keeplog , IamMain )
Create a new CallMenu object.
Parameters:
name string, name of menu
keeplog FALSE[default] do not open a log file for each call
TRUE open a log file
IamMain TRUE, the highest level menu [default]
FALSE, sub menu
Example:
decl m = new CallMenu("options");

decl m = new CallMenu("suboptions",FALSE);


 CmdLine

CallMenu :: CmdLine ( args )
Run options from command line if any, otherwise go interactive.
Parameters:
arg integer [default=1] the first command line argument to treat as a menu option. This means non-OxMenu options must appear first on the command line.
an array of options to execute as if entered from the keyboard.

If the array is empty or if the arglist is shorter than the initial value the menu enters interactive mode by calling Run(). Thus, using CmdLine does not require command line options.

The form of an options is either an integer corresponding to the indices of the menu item or the corresponding prompt as a string. If the item is a submenu then the next token in the arglist or array is the item to select from the submenu. Once a function is run (including
  oxl main opt1 [sub1 ...] opt

menu->CmdLine


 help_text

decl help_text [public]
string printed if HELP selected.

 IamMain

decl IamMain [public]
TRUE if top menu.

 make_the_call

CallMenu :: make_the_call ( item )
Call the function or enter the submenu selected.
Parameters:
item menu item

 Run

CallMenu :: Run ( )
Run the menu.
Parameters:
logoutput TRUE: create a logfile for this run.
Example:
m->Run();

 Menu

Enumerations
Anonymous enum 1 prompt, call, ItemSize
Anonymous enum 2 EXIT, HELP, QUIT, DOALL, CHOICE

 add


 CmdLine


 itemparse

static Menu :: itemparse ( token , items )
Find the menu item corresponding to the token.
Parameters:
token a string prompt or an integer index
items a list of menu items.
Returns:
integer index into the list

 items

decl items [public]
array containing menu.

 keeplog

decl keeplog [public]
log calls from this menu.

 logdir

static decl logdir [public]
Folder for logs.

 Menu

Menu :: Menu ( name , keeplog )
Create a new menu object.
Parameters:
name string, name of menu
keeplog FALSE[default] do not open a log file for each call
TRUE open a log file

 name

decl name [public]
string: menu name.

 sep

static const decl sep [public]

 SetLogDir

static Menu :: SetLogDir ( folder )
Set the static folder to place logs of calls for all menus.
Parameters:
folder string, a path. Default is "./"

 ~Menu

Menu :: ~ Menu ( )

 ParamMenu

Enumerations
Anonymous enum 1 RESET, SEND, Value

 add

ParamMenu :: add ( ... )
add parameters to the list.
Parameters:
... list of parameters each an array of size ItemSize

The first element of an item is a prompt string. The second is the default value of the parameter, which can be an integer, double or matrix. The dimensions of the matrix determines the shape to be read in (dimensions are fixed).

Example:
m->add( {"sigma",0.5}, {"Index",3}, {"Values",<0;2;3>} );

 CmdLine

ParamMenu :: CmdLine ( TargFunc , args )
Set parameter values from the command line. index=val index is an integer index = is the symbol "=" val is either an integer or double or a string containing numbers to be read in to a

 ParamMenu

ParamMenu :: ParamMenu ( name , keeplog )

 pars

decl pars [public]

 SetPars

ParamMenu :: SetPars ( TargFunc )
Set Parameters.
Parameters:
logoutput TRUE: create a logfile for this run.
Example:
m->SetPars();