CommandLiner.Getopt.getargFunction
getopt()
getargs(stypes="ss*"; stopatopt=true, mustexhaust=true)

Tiny, maximally local, and validating command line arg parsing.

Use in a while loop (while length(ARGS)>0):

  • getopt() pops and returns the next option from ARGS; if no option is found (i.e., string not starting with "–" or "-"), returns nothing (the arguments that would possibly follow are "naked").
  • getargs() returns all arguments up to the next option; must find at least 1.
  • getargs0() returns all arguments up to the next option (0 or more).
  • getarg() returns one argument (not as vector).
  • getargs("sfii") returns a vector of 4 arguments, a string, a float and two integers. Fails if more or fewer arguments are encountered, or if types do not match.
  • getargs("si*") return a vector with a string argument and 0+ ints.

if stopatopt is false, all command line strings are treated as arguments (default distinguishes between options and arguments).

if mustexhaust is false, you can read n arguments, and leave additional, "naked arguments" for later parsing (default fails if there are valid arguments left, i.e., if the next string is not an option).

CommandLiner.Getopt.getargsFunction
getopt()
getargs(stypes="ss*"; stopatopt=true, mustexhaust=true)

Tiny, maximally local, and validating command line arg parsing.

Use in a while loop (while length(ARGS)>0):

  • getopt() pops and returns the next option from ARGS; if no option is found (i.e., string not starting with "–" or "-"), returns nothing (the arguments that would possibly follow are "naked").
  • getargs() returns all arguments up to the next option; must find at least 1.
  • getargs0() returns all arguments up to the next option (0 or more).
  • getarg() returns one argument (not as vector).
  • getargs("sfii") returns a vector of 4 arguments, a string, a float and two integers. Fails if more or fewer arguments are encountered, or if types do not match.
  • getargs("si*") return a vector with a string argument and 0+ ints.

if stopatopt is false, all command line strings are treated as arguments (default distinguishes between options and arguments).

if mustexhaust is false, you can read n arguments, and leave additional, "naked arguments" for later parsing (default fails if there are valid arguments left, i.e., if the next string is not an option).

CommandLiner.Getopt.getargs0Function
getopt()
getargs(stypes="ss*"; stopatopt=true, mustexhaust=true)

Tiny, maximally local, and validating command line arg parsing.

Use in a while loop (while length(ARGS)>0):

  • getopt() pops and returns the next option from ARGS; if no option is found (i.e., string not starting with "–" or "-"), returns nothing (the arguments that would possibly follow are "naked").
  • getargs() returns all arguments up to the next option; must find at least 1.
  • getargs0() returns all arguments up to the next option (0 or more).
  • getarg() returns one argument (not as vector).
  • getargs("sfii") returns a vector of 4 arguments, a string, a float and two integers. Fails if more or fewer arguments are encountered, or if types do not match.
  • getargs("si*") return a vector with a string argument and 0+ ints.

if stopatopt is false, all command line strings are treated as arguments (default distinguishes between options and arguments).

if mustexhaust is false, you can read n arguments, and leave additional, "naked arguments" for later parsing (default fails if there are valid arguments left, i.e., if the next string is not an option).

CommandLiner.Getopt.getoptFunction
getopt()
getargs(stypes="ss*"; stopatopt=true, mustexhaust=true)

Tiny, maximally local, and validating command line arg parsing.

Use in a while loop (while length(ARGS)>0):

  • getopt() pops and returns the next option from ARGS; if no option is found (i.e., string not starting with "–" or "-"), returns nothing (the arguments that would possibly follow are "naked").
  • getargs() returns all arguments up to the next option; must find at least 1.
  • getargs0() returns all arguments up to the next option (0 or more).
  • getarg() returns one argument (not as vector).
  • getargs("sfii") returns a vector of 4 arguments, a string, a float and two integers. Fails if more or fewer arguments are encountered, or if types do not match.
  • getargs("si*") return a vector with a string argument and 0+ ints.

if stopatopt is false, all command line strings are treated as arguments (default distinguishes between options and arguments).

if mustexhaust is false, you can read n arguments, and leave additional, "naked arguments" for later parsing (default fails if there are valid arguments left, i.e., if the next string is not an option).

CommandLiner.Ext.extFunction
ext(path) -> String or nothing

Returns the path extension. Behaves similarly to Base.splitext(s)[2], but does not retain the leading '.'.

Original splitext:

  • "myfile.txt" -> ".txt"
  • "myfile." -> "."
  • "myfile" -> ""
  • ".myfile" -> ""
  • "." -> ""

New ext:

  • "myfile.txt" -> "txt"
  • "myfile." -> ""
  • "myfile" -> nothing
  • ".myfile" -> nothing
  • "." -> nothing

See also exty, hasext.

CommandLiner.Ext.extyFunction
exty(path) ->  Symbol

Returns a symbol representing the path extension:

julia> exty("myfile.jpg")
:jpeg

julia> exty("myfile.jpeg")
:jpeg

This symbol can then be used with hasext to capture all extensions in an extension group, e.g., {"jpg", "jpeg", and "jpe"}.

You can use CommandLiner.Ext.show_extgroups() to show the extension groups, and CommandLiner.Ext.register_extgroups(::Vector{Vector{String}}) to redefine them.

Use ext if you want to distinguish between special cases of empty extensions, and exty if not:

julia> (ext("myfile"), exty("myfile"))
(nothing, :_empty_)

julia> (ext("myfile."), exty("myfile."))
("", :_empty_)

You can overwrite the symbol used for missing extensions (default: :_empty_) via CommandLiner.Ext.setsym_empty().

CommandLiner.Ext.hasextFunction
hasext(path, ext::String) -> Bool
hasext(path, exty::Symbol) -> Bool

Checks a path's extension. A string must match the extension exactly:

julia> hasext("myfile.txt", "txt")
true

julia> hasext("myfile.TXT", "txt")
false

A symbol matches in an extension group, and is also case-insensitive:

julia> hasext("myfile.jpeg", :jpeg)
true

julia> hasext("myfile.jpg", :jpeg)
true

julia> hasext("myfile.JPEG", :jpeg)
true

julia> hasext("myfile.jpeg", :jpg)  # any extension in an extension group works as a symbol for that group
true

See also exty.

CommandLiner.Error.EnduserErrorType
erroruser(msg, exitcode=99)
EnduserError(msg, exitcode=99)

The function erroruser, similarly to Base.error, throws the EnduserError exception. This exception is meant to signal obvious errors to end users, for example "file 'foo' not found" or "invalid command line option '–foo'". No backtrace is output; if you catch this exception, you should also suppress it.

CommandLiner.Error.erroruserFunction
erroruser(msg, exitcode=99)
EnduserError(msg, exitcode=99)

The function erroruser, similarly to Base.error, throws the EnduserError exception. This exception is meant to signal obvious errors to end users, for example "file 'foo' not found" or "invalid command line option '–foo'". No backtrace is output; if you catch this exception, you should also suppress it.

CommandLiner.Str.extFunction
ext(path)

Returns file name extension. It has the same behavior as splitext, but without "." as prefix.

  • "file.txt" -> "txt"
  • "file." -> ""
  • "file" -> nothing
  • ".file" -> nothing
  • "." -> nothing
CommandLiner.CommandLinerModule
CommandLiner

Module with helpers:

  • option parsing; SIGPIPE; user-facing exceptions..
  • currying map and filter overrides
  • hack for reverse assignment at end of pipe chain
CommandLiner.Mainy.hushexitFunction
hushexit(f::Function)

Wraps a call to function f in a try/catch:

  • On SIGPIPE: no error msg; no backtrace; exit code 0 (for broken Unix pipes).
  • On interrupt: no error msg; no backtrace; exit code 130 (even in script).
  • On EnduserError: error msg but no backtrace; exit code is the exception's one.
  • ..rethrows otherwise.
CommandLiner.Mainy.@guardMacro
@main, @mn
@ismain
@guard

Main guard helpers:

  • @main: if run as script, calls the main function (which the user needs to provide).
  • @mn: same, but calls a (user-provided) mn function (for compat., should Julia handle main in the future).
  • @ismain: returns boolean; usage: @ismain() && myfunction().
  • @guard: shorter; usage: @guard myfunction().

@main and @mn additionally wrap the main call in hushexit, which handles SIGPIPEs and iterrupts.

Also see: hushexit.

CommandLiner.Mainy.@ismainMacro
@main, @mn
@ismain
@guard

Main guard helpers:

  • @main: if run as script, calls the main function (which the user needs to provide).
  • @mn: same, but calls a (user-provided) mn function (for compat., should Julia handle main in the future).
  • @ismain: returns boolean; usage: @ismain() && myfunction().
  • @guard: shorter; usage: @guard myfunction().

@main and @mn additionally wrap the main call in hushexit, which handles SIGPIPEs and iterrupts.

Also see: hushexit.

CommandLiner.Mainy.@mainMacro
@main, @mn
@ismain
@guard

Main guard helpers:

  • @main: if run as script, calls the main function (which the user needs to provide).
  • @mn: same, but calls a (user-provided) mn function (for compat., should Julia handle main in the future).
  • @ismain: returns boolean; usage: @ismain() && myfunction().
  • @guard: shorter; usage: @guard myfunction().

@main and @mn additionally wrap the main call in hushexit, which handles SIGPIPEs and iterrupts.

Also see: hushexit.

CommandLiner.Mainy.@mnMacro
@main, @mn
@ismain
@guard

Main guard helpers:

  • @main: if run as script, calls the main function (which the user needs to provide).
  • @mn: same, but calls a (user-provided) mn function (for compat., should Julia handle main in the future).
  • @ismain: returns boolean; usage: @ismain() && myfunction().
  • @guard: shorter; usage: @guard myfunction().

@main and @mn additionally wrap the main call in hushexit, which handles SIGPIPEs and iterrupts.

Also see: hushexit.

CommandLiner.ReverseAssignModule
1.0 |> sin |> cos |> sin |> :mynewvariable

After calling ReverseAssign.enable(), you can reverse-assign into a variable (its name given by the symbol to the right).

Meant to be used in the REPL. This hack avoids having to go back at the start of a REPL pipeline to enter some x = there.

  • The new variable lives in the Main scope.
  • This works by overwriting the function call operator for Symbol.
CommandLiner.ReverseAssign.reverseassignFunction
1.0 |> sin |> cos |> sin |> :mynewvariable

After calling ReverseAssign.enable(), you can reverse-assign into a variable (its name given by the symbol to the right).

Meant to be used in the REPL. This hack avoids having to go back at the start of a REPL pipeline to enter some x = there.

  • The new variable lives in the Main scope.
  • This works by overwriting the function call operator for Symbol.
CommandLiner.Exe.exeFunction
exe([string, strin,..]; fail=true, onlystdout=stdout, splitlines=true)

Wrapper to to run commands and grab rc/stdout/stderr.

Fails with exception on non-zero exit code if fail==true.

If onlystdout==true, returns either string or vector of strings from stdout, depending on splitlines.

If onlystdout==false, returns (rc, out, err)- or (rc, outs, errs)-tuple, again depending on split/no split.