Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clap — ESUG 2018

Damien Pollet
September 13, 2018

Clap — ESUG 2018

Clap is a command-line argument parser for Pharo

Github: https://github.com/cdlm/clap-st
Demo video: https://www.youtube.com/watch?v=VjnVZR4FMcQ

Damien Pollet

September 13, 2018
Tweet

More Decks by Damien Pollet

Other Decks in Programming

Transcript

  1. CLAP

    View Slide

  2. 3

    View Slide

  3. Damien
    sept. 2018
    PHARO
    esug clap N+1
    tool
    demo
    4
    command-
    args
    line
    ommand
    ine

    rgument
    arser
    ommand
    ine

    rgument
    arser
    ommand
    ine

    rgument
    arser
    Command
    Line

    Argument
    Parser
    C
    L

    A
    P
    C
    L

    A
    P
    C
    L

    A
    P
    C
    L

    A
    P

    View Slide

  4. demo
    5

    View Slide

  5. 6
    github.com/cdlm/clap-st
    hello! I'm Damien Pollet
    RMOD team @ Inria Lille, France
    …and I teach Java… !
    Use the command line?

    please talk to me!
    besides Clap:
    ·Coral (a looong time ago)
    ·LaTeX typography for the Pharo books
    ·fari.sh (image builder/runner)

    View Slide

  6. what
    7

    View Slide

  7. 8
    hello
    command

    View Slide

  8. 9
    'foo'
    hello
    positional

    View Slide

  9. 10
    'foo'
    --shout
    hello
    positional
    flag
    command

    View Slide

  10. 11
    'foo'
    --shout
    hello
    positional
    flag
    --repeat 42
    flag
    command
    pos.

    View Slide

  11. 12
    2nd pos.
    1st pos.
    sub-cmd
    sub-command
    command
    git -C dir remote add foo [email protected]
    pos
    flag

    View Slide

  12. flow
    13
    feedback

    to user
    application

    domain
    Queries, meanings

    +

    validations
    specification
    (= grammar)
    activation
    (= syntax tree)
    command line

    (= words)
    specification
    (= grammar)
    activation
    (= syntax tree)
    command
    flag
    positional
    command line

    (= words)
    hello --shout foo
    context
    match
    mismatch
    implicit
    Queries, meanings

    +

    validations
    feedback

    to user
    application

    domain
    !
    "

    View Slide

  13. how
    14

    View Slide

  14. specify
    15
    Positional
    Flag
    Command
    aliases
    Parameterized
    name
    description
    meaning
    defaultMeaning
    validations
    Parameter

    View Slide

  15. 16
    cmd := (ClapCommand withName: 'hello')
    description: 'Says hello';
    add: param;
    add: (ClapFlag withName: 'shout');
    meaning: [ :args | "# ]
    param := (ClapPositional withName: 'who')
    description: 'Recipient of greeting';
    defaultMeaning: [ 'world' ].

    View Slide

  16. activate
    args
    parent
    spec
    SubExpression
    Implicit
    word
    Explicit
    Parameter
    Expression
    ···Match
    Context

    View Slide

  17. 18
    parent
    match
    atName:'shout'
    Context
    #(hello esug)
    Command
    hello
    Positional
    esug
    atName:'who'
    Implicit
    context
    parent

    View Slide

  18. 19
    context := (ClapContext specification: cmd)
    arguments: #('hello' 'esug').
    "same + immediate evaluation:"
    cmd activateWith: #('hello' 'esug').

    View Slide

  19. context match.
    20
    context match atName: 'shout'.
    context match at: param.
    context match atName: 'who'.
    "a ClapPositionalMatch(esug)"
    "a ClapCommandMatch(hello esug)"
    "a ClapImplicit"

    View Slide

  20. give meaning
    21
    cmd meaning: [ :args | ].
    "#
    42
    "42"
    context match value.
    (context match atName: 'who') value. "esug"
    (context match atName: 'shout') value. "false"

    View Slide

  21. 22
    cmd meaning: [ :args |
    | recipient |
    recipient := args atName: 'who'.
    args context stdout
    << 'hello ' , recipient value;
    lf
    ]
    match of CMD
    spec
    match of positional
    meaning of positional
    (implicit matches give
    default meaning)

    View Slide

  22. ClapCommandLineExamples class >> hello

    ^
    install
    23
    (ClapCommand withName: 'hello')
    description: 'Says hello';
    add: (ClapPositional withName: 'who')
    description: 'Recipient of greeting'
    defaultMeaning: [ 'world' ].
    hello esug
    › ./pharo Pharo.image clap

    View Slide

  23. more
    24

    View Slide

  24. exit
    25
    context exitSuccess.
    Exit success signal.
    context exitFailure.
    context exit: 1.
    Exit failure signal.
    $
    %
    "OK for interactive use (catches Exit)"
    cmd activateWith: #('hello' 'esug').

    View Slide

  25. help
    26
    ClapCommand forHelp.
    ClapFlag forHelp. git commit --help
    git help commit
    documents specified sibling, or parent
    runs automatically
    meaning: [ :args |
    args atName: 'help' ifFound: [ :help |
    help value; exitSuccess ].
    documents parent
    requires explicit check

    View Slide

  26. validate
    27
    git -C dir remote add foo [email protected]
    valid repo valid
    name
    valid URL
    ↺ work in progress…

    View Slide

  27. hack
    28

    View Slide

  28. 29
    github.com/cdlm/clap-st
    ✓ basic architecture
    ✓ workspace & commandline
    ✓ pharo commands
    version & evaluate → more to port
    ↺ validations
    error handling & feedback
    → common meanings
    → Coral-like DSL
    → port fari.sh
    → shell completions…

    View Slide

  29. fin
    30

    View Slide

  30. IBM Plex
    fonts
    Clapping
    Music
    Pharo
    postcard
    Coral
    slides
    Clap demo
    (YouTube)

    View Slide