Skip to main content
Engineering LibreTexts

1.4: Anatomy of a Handler

  • Page ID
    43781
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    As we mentioned, the command line mechanism is open and can be extended. We will look now how at the handler for the eval option is defined.

    Evaluating Pharo Expressions. You can use the command line to evaluate expressions as follows: ./pharo Pharo.image eval '1+2'

    ./pharo Pharo.image eval --help
    Usage: eval [--help] <smalltalk expression>
        --help list this help message
        <smallltalk expression> a valid Smalltalk expression which is evaluated and
            the result is printed on stdout
    
    Documentation:
    A CommandLineHandler that reads a string from the command line, outputs the
        evaluated result and quits the image.
    
    This handler either evaluates the arguments passed to the image:
        $PHARO_VM my.image eval 1 + 2
    
    or it can read directly from stdin:
        echo "1+2" | $PHARO_VM my.image eval
    

    Now the handler is defined as follows: First we define a subclass of CommandLineHandler. Here BasicCodeLoader is a subclass of CommandLineHandler and EvaluateCommandLineHandler is a subclass of BasicCodeLoader.

    BasicCodeLoader subclass: #EvaluateCommandLineHandler
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'System-CommandLine'
    

    We then define the commandName on the class side as well as the method isResponsibleFor:.

    EvaluateCommandLineHandler class>>commandName
        ^ 'eval'
    
    EvaluateCommandLineHandler class>>isResponsibleFor: commandLineArguments
        "directly handle top-level -e and --evaluate options"
        commandLineArguments withFirstArgument: [ :arg|
            (#('-e' '--evaluate') includes: arg)
                ifTrue: [ ^ true ]].
    
        ^ commandLineArguments includesSubCommand: self commandName
    
    EvaluateCommandLineHandler class>>description
        ^ 'Directly evaluates passed in one line scripts'
    

    Then we define the method activate which will be executed when the option matches.

    EvaluateCommandLineHandler>>activate
        self activateHelp.
        self arguments ifEmpty: [ ^ self evaluateStdIn ].
        self evaluateArguments.
        self quit.
    

    In particular we define a class comment since this is this class comment that will be printed when the help is requested.

    If you want your image saved at the end of an evaluation script, use the --save option just after eval.


    This page titled 1.4: Anatomy of a Handler is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Alexandre Bergel, Damien Cassou, Stéphane Ducasse, Jannik Laval (Square Bracket Associates) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.