Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,739 changes: 0 additions & 2,739 deletions argparse/argparse_blackbox_test.mbt

This file was deleted.

419 changes: 0 additions & 419 deletions argparse/argparse_coverage_test.mbt

This file was deleted.

778 changes: 0 additions & 778 deletions argparse/argparse_test.mbt

This file was deleted.

90 changes: 90 additions & 0 deletions argparse/command_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
fn empty_env() -> Map[String, String] {
Map([])
}

///|
test "declarative parse basics" {
let cmd = @argparse.Command(
"demo",
flags=[FlagArg("verbose", short='v', long="verbose")],
options=[OptionArg("count", long="count", env="COUNT")],
positionals=[PositionArg("name")],
)
let matches = cmd.parse(argv=["-v", "--count", "3", "alice"], env=empty_env()) catch {
_ => panic()
}
assert_true(matches.flags is { "verbose": true, .. })
assert_true(matches.values is { "count": ["3"], "name": ["alice"], .. })
assert_true(
matches.sources is { "verbose": Argv, "count": Argv, "name": Argv, .. },
)
}

///|
test "command policies" {
let help_cmd = @argparse.Command("demo", arg_required_else_help=true)
inspect(
help_cmd.render_help(),
content=(
#|Usage: demo
#|
#|Options:
#| -h, --help Show help information.
#|
),
)

let sub_cmd = @argparse.Command("demo", subcommand_required=true, subcommands=[
Command("echo"),
])
inspect(
sub_cmd.render_help(),
content=(
#|Usage: demo <command>
#|
#|Commands:
#| echo
#| help Print help for the subcommand(s).
#|
#|Options:
#| -h, --help Show help information.
#|
),
)
try sub_cmd.parse(argv=[], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: the following required argument was not provided: 'subcommand'
#|
#|Usage: demo <command>
#|
#|Commands:
#| echo
#| help Print help for the subcommand(s).
#|
#|Options:
#| -h, --help Show help information.
#|
),
)
} noraise {
_ => panic()
}
}
238 changes: 238 additions & 0 deletions argparse/errors_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
test "unknown argument keeps suggestion in final message" {
let cmd = @argparse.Command("demo", flags=[FlagArg("verbose", long="verbose")])

try cmd.parse(argv=["--verbse"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '--verbse' found
#|
#| tip: a similar argument exists: '--verbose'
#|
#|Usage: demo [options]
#|
#|Options:
#| -h, --help Show help information.
#| --verbose
#|
),
)
} noraise {
_ => panic()
}
}

///|
test "parse error show is readable" {
let cmd = @argparse.Command(
"demo",
flags=[FlagArg("verbose", long="verbose")],
positionals=[PositionArg("name")],
)

try cmd.parse(argv=["--verbse"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '--verbse' found
#|
#| tip: a similar argument exists: '--verbose'
#|
#|Usage: demo [options] [name]
#|
#|Arguments:
#| name
#|
#|Options:
#| -h, --help Show help information.
#| --verbose
#|
),
)
} noraise {
_ => panic()
}

try cmd.parse(argv=["alice", "bob"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected value 'bob' for '<name>' found; no more were expected
#|
#|Usage: demo [options] [name]
#|
#|Arguments:
#| name
#|
#|Options:
#| -h, --help Show help information.
#| --verbose
#|
),
)
} noraise {
_ => panic()
}
}

///|
test "unknown argument suggestions are exposed" {
let cmd = @argparse.Command("demo", flags=[
FlagArg("verbose", short='v', long="verbose"),
])

try cmd.parse(argv=["--verbse"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '--verbse' found
#|
#| tip: a similar argument exists: '--verbose'
#|
#|Usage: demo [options]
#|
#|Options:
#| -h, --help Show help information.
#| -v, --verbose
#|
),
)
} noraise {
_ => panic()
}

try cmd.parse(argv=["-x"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '-x' found
#|
#| tip: a similar argument exists: '-v'
#|
#|Usage: demo [options]
#|
#|Options:
#| -h, --help Show help information.
#| -v, --verbose
#|
),
)
} noraise {
_ => panic()
}

try cmd.parse(argv=["--zzzzzzzzzz"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '--zzzzzzzzzz' found
#|
#|Usage: demo [options]
#|
#|Options:
#| -h, --help Show help information.
#| -v, --verbose
#|
),
)
} noraise {
_ => panic()
}
}

///|
test "unified error message formatting remains stable" {
let cmd = @argparse.Command("demo", options=[OptionArg("tag", long="tag")])

try cmd.parse(argv=["--oops"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '--oops' found
#|
#|Usage: demo [options]
#|
#|Options:
#| -h, --help Show help information.
#| --tag <tag>
#|
),
)
} noraise {
_ => panic()
}

try cmd.parse(argv=["--tag"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: a value is required for '--tag' but none was supplied
#|
#|Usage: demo [options]
#|
#|Options:
#| -h, --help Show help information.
#| --tag <tag>
#|
),
)
} noraise {
_ => panic()
}
}

///|
test "unknown short suggestion can be absent" {
let cmd = @argparse.Command("demo", disable_help_flag=true, options=[
OptionArg("name", long="name"),
])

try cmd.parse(argv=["-x"], env=empty_env()) catch {
err =>
inspect(
err,
content=(
#|error: unexpected argument '-x' found
#|
#|Usage: demo [options]
#|
#|Options:
#| --name <name>
#|
),
)
} noraise {
_ => panic()
}
}

///|
test "Debug for argparse enums" {
@debug.debug_inspect(@argparse.FlagAction::SetTrue, content="SetTrue")
@debug.debug_inspect(@argparse.FlagAction::Count, content="Count")
@debug.debug_inspect(@argparse.OptionAction::Set, content="Set")
@debug.debug_inspect(@argparse.OptionAction::Append, content="Append")
}
Loading
Loading