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)
  } 
}

public 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.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 myflagsstd/os/flags/myflags: V(
  verbosestd/os/flags/verbose: (myflags : myflags) -> bool : boolstd/core/bool: V = Falsestd/core/False: bool,
  versionstd/os/flags/version: (myflags : myflags) -> bool : boolstd/core/bool: V = Falsestd/core/False: bool, 
  namestd/os/flags/name: (myflags : myflags) -> string : stringstd/core/string: V = "",
  outputstd/os/flags/output: (myflags : myflags) -> string : stringstd/core/string: V = "",
  argumentsstd/os/flags/arguments: (myflags : myflags) -> list<string> : liststd/core/list: V -> V<stringstd/core/string: V> = []
)

val myflags : liststd/core/list: V -> V<flagstd/os/flags/flag: V -> V<myflagsstd/os/flags/myflags: V>>
  = [ 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-versionstd/os/flags/set-version: (t : myflags, v : bool) -> myflags),     "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-verbosestd/os/flags/set-verbose: (t : myflags, v : bool) -> myflags),     "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-outputstd/os/flags/set-output: (t : myflags, mbs : maybe<string>) -> myflags,"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-namestd/os/flags/set-name: (t : myflags, name : string) -> myflags,"USER"),  "only show USER files" ),
    ]

fun set-namestd/os/flags/set-name: (t : myflags, name : string) -> myflags( t : myflagsstd/os/flags/myflags: V, namestd/os/flags/name: (myflags : myflags) -> string )  { t(namestd/os/flags/name: (myflags : myflags) -> string = namestd/os/flags/name: (myflags : myflags) -> string) }
fun set-verbosestd/os/flags/set-verbose: (t : myflags, v : bool) -> myflags( t : myflagsstd/os/flags/myflags: V, v )  { t(verbosestd/os/flags/verbose: (myflags : myflags) -> bool = v) }
fun set-versionstd/os/flags/set-version: (t : myflags, v : bool) -> myflags( t : myflagsstd/os/flags/myflags: V, v )  { t(versionstd/os/flags/version: (myflags : myflags) -> bool = v) }
fun set-outputstd/os/flags/set-output: (t : myflags, mbs : maybe<string>) -> myflags( t : myflagsstd/os/flags/myflags: V, mbs : maybestd/core/maybe: V -> V<stringstd/core/string: V> ) : myflagsstd/os/flags/myflags: V
{
  match(mbs) {
    Nothingstd/core/Nothing: forall<a> maybe<a> -> t(outputstd/os/flags/output: (myflags : myflags) -> string = "stdout")
    Juststd/core/Just: forall<a> (value : a) -> maybe<a>(s) -> t(outputstd/os/flags/output: (myflags : myflags) -> string = s)
  } 
}

public 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>) -> total (a, list<string>, list<string>)( Myflags(), myflags, cmdargs )
  if (errs.nil?std/core/nil?: forall<a> (list : list<a>) -> bool) {
    println( "\nsuccess!" );
    println( "flags: " + flags.show-flagsstd/os/flags/show-flags: (o : myflags) -> string)
    println( "arguments: " + args.join(" ") );
    if (flags.versionstd/os/flags/version: (myflags : myflags) -> bool) 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-flagsstd/os/flags/show-flags: (o : myflags) -> string( ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b) : myflagsstd/os/flags/myflags: V ) {
  "{" + ["verbose=" + ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).verbosestd/os/flags/verbose: (myflags : myflags) -> bool.show,
         "version=" + ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).versionstd/os/flags/version: (myflags : myflags) -> bool.show,
         "name=" + ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).namestd/os/flags/name: (myflags : myflags) -> string.show,
         "output=" + ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).outputstd/os/flags/output: (myflags : myflags) -> string.show,
         "arguments=" + ostd/core/o: forall<a,b,c,e> (f : (a) -> e b, g : (c) -> e a) -> ((x : c) -> e b).argumentsstd/os/flags/arguments: (myflags : myflags) -> list<string>.join(",")].join(";") + "}"
}

.

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

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.

con Wrap(wrap : (stringstd/core/string: V) -> a)

Wrap each non-flag argument into an flag.

Specifies the argument of an flag.

An flagal argument.

con Req(parse : (a, stringstd/core/string: V) -> ahelp : stringstd/core/string: V)

A required argument.

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

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

Parse the command line arguments args (see std/env/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.

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:".