CommandLiner.Getopt.getarg
— Functiongetopt()
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.getargs
— Functiongetopt()
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.getargs0
— Functiongetopt()
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.getopt
— Functiongetopt()
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.ext
— Functionext(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.exty
— Functionexty(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.hasext
— Functionhasext(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.EnduserError
— Typeerroruser(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.erroruser
— Functionerroruser(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.ext
— Functionext(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.CommandLiner
— ModuleCommandLiner
Module with helpers:
- option parsing; SIGPIPE; user-facing exceptions..
- currying
map
andfilter
overrides - hack for reverse assignment at end of pipe chain
CommandLiner.Mainy.hushexit
— Functionhushexit(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.@guard
— Macro@main, @mn
@ismain
@guard
Main guard helpers:
@main
: if run as script, calls themain
function (which the user needs to provide).@mn
: same, but calls a (user-provided)mn
function (for compat., should Julia handlemain
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.@ismain
— Macro@main, @mn
@ismain
@guard
Main guard helpers:
@main
: if run as script, calls themain
function (which the user needs to provide).@mn
: same, but calls a (user-provided)mn
function (for compat., should Julia handlemain
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.@main
— Macro@main, @mn
@ismain
@guard
Main guard helpers:
@main
: if run as script, calls themain
function (which the user needs to provide).@mn
: same, but calls a (user-provided)mn
function (for compat., should Julia handlemain
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.@mn
— Macro@main, @mn
@ismain
@guard
Main guard helpers:
@main
: if run as script, calls themain
function (which the user needs to provide).@mn
: same, but calls a (user-provided)mn
function (for compat., should Julia handlemain
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.ReverseAssign
— Module1.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.reverseassign
— Function1.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.exe
— Functionexe([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.