Os.program
func Os.program(): stringOs.program returns the path of the program's root module as the command was given it, as a shell's $0 is: ./tool.ant when a #! line runs it, tool.ant after ant run tool.ant. It is "" in the REPL.
Details
- The path is the one the command named, as it was typed:
Os.programdoes not make it absolute or clean it, soant run ./tool.antgives"./tool.ant". - It names the script, the file
ant runstarted, even when a module that the script imports calls it.
Basic examples
Print the path of the running program, here run as ant run main.ant:
println(Os.program())main.antRun as ant run ./main.ant, the path keeps the ./:
println(Os.program())./main.antScope
A program in another directory is named with that directory, relative to where the command ran:
println(Os.program())
println(Path.base(Os.program()))tools/report.ant
report.antAn imported module sees the path of the program that imports it, not its own:
// prefix returns msg prefixed with the program's name.
export func prefix(msg: string): string { "${Path.base(Os.program())}: ${msg}" }
// name returns the path the program was started with.
export func name(): string { Os.program() }import "./log.ant" { name, prefix }
println(prefix("starting"))
println(name())main.ant: starting
main.antApplications
Name the script in its usage line, as it was run, so the line is right however the file is named:
let args = Os.args()
if args.isEmpty() {
eprintln("usage: ${Path.base(Os.program())} <file>...")
Os.exit(2)
}
println(args)Prefix error messages with the program's name, as Unix tools do, so a message read in a log or a pipeline says where it came from:
func countLines(path: string): Result[int, Error] {
let text = try File.read(path)
Result.ok(text.lines().length())
}
let lines = countLines("missing.txt")
if lines is Err {
eprintln("${Path.base(Os.program())}: ${lines.error.message}")
Os.exit(1)
}
println(lines.value)Read a file kept next to the script, wherever the command runs from. Path.dir of the program path is the script's directory:
let dir = Path.dir(Os.program())
let words = try File.read(Path.join([dir, "words.txt"]))
println("${words.lines().length()} words in ${dir}")2 words in toolsProperties and relations
The program path is not one of the Os.args, and it is relative when the command named a relative path:
assert(Os.program() == "main.ant")
assert(Os.args() == ["a", "b"])
assert(!Os.args().contains(Os.program()))
assert(!Path.isAbs(Os.program()))Possible issues
The path is relative to the directory the command ran from. A child started in another directory, with dir:, reads it against that directory instead and finds a different file or none. Make the path absolute first with Path.abs:
assert(!Path.isAbs(Os.program()))
let script = try Path.abs(Os.program())
assert(Path.isAbs(script))Unlike argv[0] in C or sys.argv[0] in Python, Os.args()[0] is the first argument, not the program path. Use Os.program for the path:
println(Os.args()[0])
println(Os.program())input.txt
main.antSee also
Os.args · Os.exit · Os.cwd · Path.base · Path.dir · Path.abs · eprintln
