Skip to content

std/flag ​

Index ​

type Int
type Uint
type Float
type Bool
type String
trait Common
struct Flag[T]
    fn Name(*self): string
    fn What(*self): string
    fn Reset(mut *self)
    fn Short(*self): rune
struct Set
    fn New(): &Set
    fn FindFlag(mut *self, name: string): Common
    fn FindFlagShort(mut *self, name: rune): Common
    fn Flags(mut *self): []Common
    fn Parse(mut *self, args: []string)!: []string
    fn Reset(mut *self)
    fn Add[T: i64 | u64 | f64 | bool | string](mut *self, name: string, short: rune, default: T, what: string): &T
    fn AddVar[T: i64 | u64 | f64 | bool | string](mut *self, mut var: &T, name: string, short: rune, what: string)

Int ​

jule
type Int = &Flag[i64]

Flag for i64 type.

Uint ​

jule
type Uint = &Flag[u64]

Flag for u64 type.

Float ​

jule
type Float = &Flag[f64]

Flag for f64 type.

Bool ​

jule
type Bool = &Flag[bool]

Flag for bool type.

String ​

jule
type String = &Flag[string]

Flag for string type

Common ​

jule
trait Common {
	// Returns name of flag.
	fn Name(*self): string

	// Returns short name of flag.
	fn Short(*self): rune

	// Returns description of flag.
	fn What(*self): string

	// Resets data to default.
	fn Reset(mut *self)
}

Common behaviors of flags.

Flag ​

jule
struct Flag[T] {
	// NOTE: contains filtered hidden or unexported fields
}

A Flag for FlagSet.

Implemented Traits ​

  • Common

Name ​

jule
fn Name(*self): string

What ​

jule
fn What(*self): string

Reset ​

jule
fn Reset(mut *self)

Short ​

jule
fn Short(*self): rune

Set ​

jule
struct Set {
	// NOTE: contains filtered hidden or unexported fields
}

Flag parser for command-line arguments.

Syntax:

Long names can be used with double dash (--). Short names can be
used with a single dash (-). When Boolean flags are used, they use
the opposite of their default values. Floating-point values are the
same as the [ParseFloat] function provided by the "std/conv" package.
Decimal, octal, binary and hexadecimal formats are supported for
signed and unsigned integer types. String types accept values ​​directly.

Octal values are represented by starts with 0o or 0 prefix.
Hexadecimal values are represented by starts with 0x prefix.
Binary values are represented by starts with 0b prefix.

A space is required to give a value. When a single dash (-) is used,
all following characters are considered short names and thus collective
use is allowed. If the short name flags used need values, the values ​
should follow respectively.

New ​

jule
fn New(): &Set

Returns new flagset.

FindFlag ​

jule
fn FindFlag(mut *self, name: string): Common

Returns flag by name, returns nil if not exist.

FindFlagShort ​

jule
fn FindFlagShort(mut *self, name: rune): Common

Returns flag by short name, returns nil if not exist.

Flags ​

jule
fn Flags(mut *self): []Common

Returns all flags.

Parse ​

jule
fn Parse(mut *self, args: []string)!: []string

Parse arguments and process flags. Returns non-flag content. Error is always string and holds error message.

Reset ​

jule
fn Reset(mut *self)

Resets all flags to default value.

Add ​

jule
fn Add[T: i64 | u64 | f64 | bool | string](mut *self, name: string, short: rune, default: T, what: string): &T

Adds new flag and returns allocated reference variable. Panics if name or short name is alreadys exist. Zero (0) short names will be ignored. Panics if used unsupported type.

AddVar ​

jule
fn AddVar[T: i64 | u64 | f64 | bool | string](mut *self, mut var: &T, name: string, short: rune, what: string)

Same with the Add method but do not allocates new reference, uses existing.