nova-std 0.3.0

The Nova language's standard library sources (internal to the nova toolchain)
Documentation
//! The running program: its command-line arguments, and ending it with an
//! exit status.
//!
//! `args()` returns the arguments the program was started with, and
//! `exit(code)` ends it at once with the status `code`. Every module sees
//! these names without an import, and a module's own `args` or `exit`
//! shadows the one here.
//!
//! Reading the program's arguments. `exit` ends the program at once, so the
//! example does not call it:
//!
//! ```
//! let argv = args()
//! // Index 0 names the program; the arguments it was given start at index 1.
//! assert(argv.len() >= 1, "index 0 is always there")
//! let name = argv.get(1).unwrap_or("world") // `get` gives None past the end
//! println("hello, ${name}")
//!
//! // Each call returns a new Vec, so changing one changes no other.
//! let mut mine = args()
//! mine.push("--verbose")
//! assert_eq(args().len(), argv.len())
//! ```

// Nova standard library -- the running process: its arguments, and a way to
// end it with a status.
//
// Compiled as an implicit module and glob-imported into every user module, so
// `args()` and `exit(1)` need no `import` (docs/adr/0004-stdlib-compile-model.md).
// A program's own `args` or `exit` shadows these.
//
// Of `nova-spec/20-STDLIB.md`'s module-index entry, "spawn, env, args", only
// `args` ships, with `exit`; `spawn` and `env` do not exist. See that file's
// section 17, and docs/adr/0023-program-arguments.md.

// The program's arguments. Index 0 is the program itself: under
// `nova run [FILE] -- ARGS` it is FILE as written (`src/main.nova` when
// defaulted), and in a built executable it is whatever the OS reports. The
// arguments after `--`, or a built executable's own, follow from index 1. One
// that is not valid Unicode has U+FFFD in place of its bad bytes.
/// Returns the program's command-line arguments.
///
/// Index 0 names the program: under `nova run FILE -- ARGS` it is FILE as
/// written, or without FILE the path of the `src/main.nova` it runs; in a
/// built executable it is whatever the operating system reports. The
/// arguments follow from index 1: under `nova run`, those after `--`. An
/// argument that is not valid Unicode has U+FFFD in place of its bad bytes.
/// Each call returns a new `Vec`, so changing one changes no other.
pub fn args() -> Vec<String> {
    let mut v: Vec<String> = Vec::new()
    let n = process_arg_count()
    let mut i = 0
    while i < n {
        v.push(process_arg(i))
        i = i + 1
    }
    v
}

// Ends the process now with `code`, after flushing stdout. Tasks still running
// are not joined. The OS truncates `code`: Unix keeps its low 8 bits, so
// `exit(256)` is 0 and `exit(-1)` is 255; Windows keeps 32. Under `nova run` it
// ends the `nova` process itself.
//
// Typed `-> ()` because Nova has no never type: code after `exit(..)`
// type-checks, and never runs.
/// Ends the program at once with the exit status `code`, after flushing
/// standard output.
///
/// Tasks still running are not waited for. The operating system keeps only
/// part of `code`: Unix its low 8 bits, so `exit(256)` gives 0 and `exit(-1)`
/// 255, and Windows 32 bits. Under `nova run` it ends the `nova` process
/// itself. Code after a call to `exit` never runs.
pub fn exit(code: Int) { process_exit(code) }