std/os/flags▲toc

Parsing of command line flags.

For example:

struct myflags(
  verbose : bool = False,
  version : bool = False,
  name : string = "",
  output : string = "",
  arguments : list<string> = []
)

val myflags : list<flag<myflags>>
  = [ Flag( "V?", ["version"], Bool(set-version),     "display version information" ),
      Flag( "v",  ["verbose"], Bool(set-verbose),     "verbosely list files"),
      Flag( "o",  ["output"],  Opt(set-output,"FILE"),"use FILE for dump" ),
      Flag( "n",  ["name"],    Req(set-name,"USER"),  "only show USER files" ),
    ]

fun set-name( t : myflags, name )  { t(name = name)
fun set-verbose( t : myflags, v )  { t(verbose = v)
fun set-version( t : myflags, v )  { t(version = v)
fun set-output( t : myflags, mbs : maybe<string> ) : myflags

  match(mbs)
    Nothing -> t(output = "stdout")
    Just(s) -> t(output = s)


pub fun test( cmdargs )

  val header = "usage:\n program [options] files\n\noptions:"
  // testflags.usageInfo( header ).println
  val (flags,args,errs) = parse( Myflags(), myflags, cmdargs )
  if (errs.is-nil)
    println( "\nsuccess!" );
    println( "flags: " ++ flags.show-flags)
    println( "arguments: " ++ args.join(" ") );
    if (flags.version) myflags.usage(header).println

  else
    println( errs.join("\n") ++ "\n" ++ myflags.usage(header) )


fun show-flags( o : myflags )
  "{" ++ ["verbose=" ++ o.verbose.show,
         "version=" ++ o.version.show,
         "name=" ++ o.name.show,
         "output=" ++ o.output.show,
         "arguments=" ++ o.arguments.join(",")].join(";") ++ "}"

struct myflags(
  verbose : boolstd/core/types/bool: V = Falsestd/core/types/False: bool,
  version : boolstd/core/types/bool: V = Falsestd/core/types/False: bool,
  name : stringstd/core/types/string: V = "",
  output : stringstd/core/types/string: V = "",
  arguments : liststd/core/types/list: V -> V<stringstd/core/types/string: V> = []
)

val myflags : liststd/core/types/list: V -> V<flagstd/os/flags/flag: V -> V<myflags>>
  = [ Flagstd/os/flags/Flag: forall<a> (short-names : string, long-names : list<string>, parser : flag-parser<a>, help : string) -> flag<a>( "V?", ["version"], Bool(set-version),     "display version information" ),
      Flagstd/os/flags/Flag: forall<a> (short-names : string, long-names : list<string>, parser : flag-parser<a>, help : string) -> flag<a>( "v",  ["verbose"], Bool(set-verbose),     "verbosely list files"),
      Flagstd/os/flags/Flag: forall<a> (short-names : string, long-names : list<string>, parser : flag-parser<a>, help : string) -> flag<a>( "o",  ["output"],  Optstd/os/flags/Opt: forall<a> (parse : (a, maybe<string>) -> a, help : string) -> flag-parser<a>(set-output,"FILE"),"use FILE for dump" ),
      Flagstd/os/flags/Flag: forall<a> (short-names : string, long-names : list<string>, parser : flag-parser<a>, help : string) -> flag<a>( "n",  ["name"],    Reqstd/os/flags/Req: forall<a> (parse : (a, string) -> a, help : string) -> flag-parser<a>(set-name,"USER"),  "only show USER files" ),
    ]

fun set-name( t : myflags, name )  { t(name = name)
fun set-verbose( t : myflags, v )  { t(verbose = v)
fun set-version( t : myflags, v )  { t(version = v)
fun set-output( t : myflags, mbs : maybestd/core/types/maybe: V -> V<stringstd/core/types/string: V> ) : myflags

  match(mbs)
    Nothingstd/core/types/Nothing: forall<a> maybe<a> -> t(output = "stdout")
    Juststd/core/types/Just: forall<a> (value : a) -> maybe<a>(s) -> t(output = s)


pub fun teststd/os/flags/test: (cmdargs : list<string>) -> console ()( cmdargs )

  val header = "usage:\n program [options] files\n\noptions:"
  // testflags.usageInfo( header ).println
  val (flags,args,errs) = parsestd/os/flags/parse: forall<a> (initial : a, flags : list<flag<a>>, args : list<string>, ordering : ? (flag-order<a>)) -> (a, list<string>, list<string>)( Myflags(), myflags, cmdargs )
  if (errs.is-nilstd/core/types/is-nil: forall<a> (list : list<a>) -> bool)
    println( "\nsuccess!" );
    println( "flags: " ++ flags.show-flags)
    println( "arguments: " ++ args.join(" ") );
    if (flags.version) myflags.usagestd/os/flags/usage: forall<a> (flags : list<flag<a>>, header : ? string) -> string(header).println

  else
    println( errs.join("\n") ++ "\n" ++ myflags.usagestd/os/flags/usage: forall<a> (flags : list<flag<a>>, header : ? string) -> string(header) )


fun show-flags( ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b) : myflags )
  "{" ++ ["verbose=" ++ ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).verbose.show,
         "version=" ++ ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).version.show,
         "name=" ++ ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).name.show,
         "output=" ++ ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).output.show,
         "arguments=" ++ ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).arguments.join(",")].join(";") ++ "}"

.

struct flagstd/os/flags/flag: V -> V<a>(short-names : stringstd/core/types/string: Vlong-names : liststd/core/types/list: V -> V<stringstd/core/types/string: V>parser : flag-parserstd/os/flags/flag-parser: V -> V<a>help : stringstd/core/types/string: V)

Specifies a single command line flag For example: flag("h?",["help"],Bool(flag/helpstd/os/flags/flag/help: forall<a> (flag : flag<a>) -> string),"show help information").

Automatically generated. Retrieves the help constructor field of the flagstd/os/flags/flag: V -> V type.

Automatically generated. Retrieves the long-names constructor field of the flagstd/os/flags/flag: V -> V type.

Automatically generated. Retrieves the parser constructor field of the flagstd/os/flags/flag: V -> V type.

Automatically generated. Retrieves the short-names constructor field of the flagstd/os/flags/flag: V -> V type.

type flag-orderstd/os/flags/flag-order: V -> V<a>

Specifies how to handle flags that follow non-flag command line arguments.

con Permute

Allow flags to be permuted with non-flag arguments (default).

Flags following non-flag arguments are treated as arguments.

Wrap each non-flag argument into an flag.

type flag-parserstd/os/flags/flag-parser: V -> V<a>

Specifies the argument of an flag.

con Bool(default : (a, boolstd/core/types/bool: V) -> a)

For a flag foo Automatically enables forms --no-foo and --foo=true|false.

An flagal argument.

A required argument.

Automatically generated. Tests for the Bool constructor of the flag-parserstd/os/flags/flag-parser: V -> V type.

fun parse( initial : a, flags : liststd/core/types/list: V -> V<flagstd/os/flags/flag: V -> V<a>>, args : liststd/core/types/list: V -> V<stringstd/core/types/string: V>, ordering : ? (flag-orderstd/os/flags/flag-order: V -> V<a>) ) : (a, liststd/core/types/list: V -> V<stringstd/core/types/string: V>, liststd/core/types/list: V -> V<stringstd/core/types/string: V>)

Parse the command line arguments args (see get-args) according to the flag descriptions flags. Takes an flagal argument ordering (=Permutestd/os/flags/Permute: forall<a> flag-order<a>) that specifies how optare handled that follow non-flag arguments. Returns three lists: the list of parsed flags, a list of non-flag arguments, and a list of potential error messages.

fun test( cmdargs : liststd/core/types/list: V -> V<stringstd/core/types/string: V> ) : consolestd/core/console/console: X ()
fun usage( flags : liststd/core/types/list: V -> V<flagstd/os/flags/flag: V -> V<a>>, header : ? stringstd/core/types/string: V ) : stringstd/core/types/string: V

Return a nicely formatted string describing the usage of a command, consisting of a header followed by the descriptions of the flags. The default header is "usage:\n program [flags] arguments\n\nflags:".

private import std/core/typesstd/core/types, std/core/hndstd/core/hnd, std/core/exnstd/core/exn, std/core/boolstd/core/bool, std/core/orderstd/core/order, std/core/charstd/core/char, std/core/intstd/core/int, std/core/vectorstd/core/vector, std/core/stringstd/core/string, std/core/sslicestd/core/sslice, std/core/liststd/core/list, std/core/maybestd/core/maybe, std/core/eitherstd/core/either, std/core/tuplestd/core/tuple, std/core/showstd/core/show, std/core/debugstd/core/debug, std/core/delayedstd/core/delayed, std/core/consolestd/core/console, std/corestd/core, std/os/envstd/os/env