Input 序言:在shell提示符上显示帮助

Input 序言:在shell提示符上显示帮助,input,prolog,swi-prolog,Input,Prolog,Swi Prolog,我将在prolog程序外壳中读取连续提示中的一些值,我希望用户在要求输入时能够获得帮助消息。情况将是: 征求意见 如果input='help',则显示帮助消息并再次请求相同的输入 如果input/=“help”,则分配值并成功离开 到目前为止我所做的: ask_input( Question, Value ) :- write( Question ), % Please enter ... : read( ReadValue ), ( ReadValue = 'help

我将在prolog程序外壳中读取连续提示中的一些值,我希望用户在要求输入时能够获得帮助消息。情况将是:

  • 征求意见
  • 如果
    input='help'
    ,则显示帮助消息并再次请求相同的输入
  • 如果
    input/=“help”
    ,则分配
    并成功离开
  • 到目前为止我所做的:

    ask_input( Question, Value ) :-
        write( Question ), % Please enter  ... :
        read( ReadValue ),
        ( ReadValue = 'help' ->
            write( 'Help message...' ),
            ask_input( Question, Value )
        ;   Value = ReadValue
        ).
    

    显然,上面的代码不起作用。它将在条件内的
    ask\u输入上失败。

    我这样做了,它似乎工作了:

    ask_question( Question, Value ) :-
        write( Question ), nl,
        read( ReadValue ),
        ask_question2( Question, ReadValue, NewValue ),
        Value = NewValue.
    
    ask_question2( Question, ReadValue, NewValue ) :-
        ReadValue = 'help',
        write( 'Help message ...' ), nl,
        ask_question( Question, NewValue ).
    
    ask_question2( _, Value, Value ).
    

    我会写
    ask\u question2(问题,帮助,NewValue):-
    然后删除
    ReadValue='help'
    行。请注意,ask\u question2/3的第二个子句在回溯ReadValue='help'时会成功。我想得更多,我越是认为这种方法可以通过接受输入然后处理来简化和避免问题。“帮助”功能只是一个处理程序。默认处理程序将显示一个错误,并可能显示“帮助”消息。围绕这一点构建您的输入循环,而不是试图处理无效的输入,并在更深层次上提供帮助。