Skip to content

9. Asking the user ​

Sometimes a program should ask rather than fail: a user at a terminal may not know the schemes heat offers. FLAP's optional menu type asks a numbered question; heat uses it only with --interactive, and only when --scheme is missing:

f90
if (interactive .and. .not.cli%is_passed(switch='--scheme')) then
  call m%init(question='Time scheme?')
  call m%add_option(text='fe: explicit Euler', is_default=.true.)
  call m%add_option(text='cn: Crank-Nicolson')
  call m%run(choice, merror)
  if (merror /= 0) stop 1, quiet=.true.   ! no answer (a batch job): stop rather than guess
  scheme = merge('fe', 'cn', choice == 1)
  write(*, '(A)')
endif
$ printf '2\n' | heat --interactive
1) *fe: explicit Euler
2) cn: Crank-Nicolson
Time scheme? 
heat: scheme cn
$ heat --scheme fe --interactive
heat: scheme fe

(In a terminal the answer 2 is echoed after the question; here it comes from printf.)

In a batch job there is nobody to answer: the menu sees the end of its input at once and returns an error, and heat stops instead of guessing:

$ heat --interactive < /dev/null
1) *fe: explicit Euler
2) cn: Crank-Nicolson
Time scheme? error: end of input, no response
[exit status 1]

The parser never asks anything: menus are a separate module, used only where the program decides to.

What you learned

menu: options, a default, an answer from standard input, safe behaviour in batch jobs. Reference: Interactive Menus.

That is the whole tour. For quick answers, see the cookbook.