Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CLImate - a CLI generating library

CLImate (the same pronunciation as climate) is a CLI generating library. Provising CLI definitions, then the library handles argument check, generates usage message.

Examples

APIs

Library (climate)

Syntax climate name command ...
Auxiliary syntax group command ...
Auxiliary syntax arguments (required ...) option ...
Auxiliary syntax options option ...

Builds climate object. The syntax looks like this

(climate $name
  ($command "usage"
    (group ($sub-command0 "usage of $sub-command0" $processor0
             (arguments 
               ((arg0 "arg0 description")
                 arg1)  ;; description can be omit
               ;; keyword (short long) argument-required default-value usage
               (:opt0 (#\o "option0") #t #f "opt0 descrition")
               (:opt1 (#\O "option1") #f #f "opt1 descrition")))
            ($sub-command1 "usage of $sub-command1" $processor1
             (options 
               (:opt0 (#\o "option0") #t #f "opt0 descrition")
               (:opt1 (#\O "option1") #f #f "opt1 descrition"))))))

This generates below CLI commands

  • $name $sub-command0 {arg0} {arg1} [-o {opt0}] [-O {opt1}]
  • $name $sub-command1 [-o {opt0}] [-O {opt1}]

The $processor must be a procesure which accepts the arguments described in the CLI specification. For example, for the $sub-command0, then it must be like this

(define (processor0 arg0 arg1 :key opt0 opt1) ...)
Procedure climate? obj

Returns #t if the given obj is climate object, otherwise #f.

Procedure execute-climate climate args

Executes the given climate with the given args

The args must be a list of strings represents the command line argument

(define example-cli (climate ...))
;; using main
(define (main args)
  (let ((result (execute-climate example-cli (cdr args))))
    (display (result-value result)) (newline)
	(exit (if (result-success? result) 0 1))))
Procedure result? obj

Returns #t if the given obj is climate result object, otherwise #f.

Procedure result-success? result

Returns #t if the execution is successful otherwise #f.

Procedure result-value result

Retrieves the result value. When the execution failed due to an unexpected error, then usage message is returned.

Executor APIs

The below APIs are also exported from (climate)

Library (climate input)

Input parsing utilties library.

Procedure argument->input-port arg

The arg must be a string.

Converts the given arg into a binary input port. This procedure applies some conversion rules if arg is;

  1. A string -, then (standard-input-port) is returned
  2. A string starts with @, then reading from a file. e.g. If arg is @input.txt, then input.txt is read
  3. A string doesn't apply the above, then (open-bytevector-input-port (string->utf8 _arg_) is applied.
Procedure argument->string-content arg

Converts given arg to string applying the rule of argument->input-port.

Procedure call-with-argument-input-port arg proc :key (transcoder #f)

Applying given proc with the result of argument->input-port. If the keyword argument transcoder is specified, then proc receives a transcoded port.

Procedure parse-attributed-argument arg :optional (default #f)

Split the given arg with | and returns 2 values. If the arg doesn't contain |, then arg and default are returned.

If arg is "foo|bar", then the return values are "foo" and "bar".
If arg is "foo", then the return values are "foo" and #f.
If arg is "foo" and default is "buz", then return values are "foo" and "buz"

About

CLI generator

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages