Skip to content

Commit

Permalink
std/flag: refactor documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Jan 17, 2025
1 parent 6a11f73 commit 8ee592a
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions std/flag/flag.jule
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ trait CommonFlag {
fn Reset(mut self)
}

// A Flag for FlagSet.
struct Flag[T] {
name: str
short: rune
Expand Down Expand Up @@ -53,6 +54,23 @@ type BoolFlag = &Flag[bool]
type StrFlag = &Flag[str]

// 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.
struct FlagSet {
flags: []CommonFlag
}
Expand Down Expand Up @@ -95,23 +113,6 @@ impl FlagSet {
// Parse arguments and process flags.
// Returns non-flag content.
// Exceptional always is string and holds error message.
//
// 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.
fn Parse(mut self, args: []str)!: []str {
mut ap := argParser{
set: self,
Expand Down Expand Up @@ -150,20 +151,13 @@ impl FlagSet {
// Panics if name or short name is alreadys exist.
// Zero (0) short names will be ignored.
// Panics if used unsupported type.
//
// Supported types are:
// - i64
// - u64
// - f64
// - str
// - bool
fn Add[T: i64 | u64 | f64 | bool | str](mut self, name: str, short: rune, default: T, what: str): &T {
mut flag := self.addFlagCommon[T](name, short, what)
flag.data = new(T, default)
ret flag.data
}

// Same with add method but do not allocates new reference, uses existing.
// Same with the Add method but do not allocates new reference, uses existing.
fn AddVar[T: i64 | u64 | f64 | bool | str](mut self, mut var: &T, name: str, short: rune, what: str) {
if var == nil {
panic("flag: FlatSet.AddVar[T]: variable is nil")
Expand Down

0 comments on commit 8ee592a

Please sign in to comment.