Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admercs committed Mar 28, 2024
1 parent feeca31 commit 58d333c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,7 +1,7 @@
name = "SimpleArgParse"
uuid = "4c739d64-e22d-44e1-ada1-53a1c3c0e953"
authors = ["Adam Erickson <adam.michael.erickson@gmail.com>"]
version = "1.0.1"
version = "1.1.0"

[deps]
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -2,7 +2,7 @@

[![DOCS][docs-img]][docs-url] [![CI][CI-img]][CI-url] [![CODECOV][codecov-img]][codecov-url]

A hackable, single-file, 320-line Julia package for command-line argument parsing. `SimpleArgParse` offers 95% of the functionality of `ArgParse` using ~10% of the lines-of-code (LOC).
A hackable, single-file, 355-line Julia package for command-line argument parsing. `SimpleArgParse` offers 95% of the functionality of `ArgParse` using ~10% of the lines-of-code (LOC).

Does this need to be more complicated?

Expand Down Expand Up @@ -110,6 +110,12 @@ That is about as simple as it gets and closely follows Python's [`argparse`](htt

## Changelog

## Release 1.1.0

- SHA-256 hash key replaced with a simple counter.
- New PromptedArgumentParser data structure and associated helper methods.
- Minor formatting changes.

### Release 1.0.0

- Changed hashmap key from 8-bit to 16-bit to reduce collision likelihood.
Expand Down
31 changes: 15 additions & 16 deletions src/SimpleArgParse.jl
@@ -1,12 +1,9 @@
module SimpleArgParse

export ArgumentParser, add_argument!, add_example!, generate_usage, help, parse_args!,
get_value, set_value!, has_key, get_key, colorize,
colorprint, args_pairs, PromptedParser,
get_value, set_value!, has_key, get_key, colorize, colorprint, args_pairs, PromptedParser,
keys

# using Base
# using SHA: sha256
using OrderedCollections: OrderedDict

###
Expand Down Expand Up @@ -319,14 +316,24 @@ function colorize(text::AbstractString; color::AbstractString="default", backgro
return "\033[" * code_string * "m" * text * "\033[0m"
end

# # # # # # # #

function colorprint(text, color="default", newline=true; background=false, bright=false)
function colorprint(text, color="default", newline=true; background=false, bright=false)
:Nothing
print(colorize(text; color, background, bright))
newline && println()
end

argpair(s, args) = Symbol(s) => get_value(args, s)
"Prompted command-line argument parser with key-value stores and attributes."
@kwdef mutable struct PromptedParser
parser::ArgumentParser = ArgumentParser()
color::String = "default"
introduction::String = ""
prompt::String = "> "
end

# PromptedParser helper methods.
function argpair(s, args)
return Symbol(s) => get_value(args, s)
end

function args_pairs(args::ArgumentParser)
allkeys = keys(args)
Expand All @@ -336,13 +343,6 @@ function args_pairs(args::ArgumentParser)
return ps
end

@kwdef mutable struct PromptedParser
parser::ArgumentParser = ArgumentParser()
color::String = "default"
introduction::String = ""
prompt::String = "> "
end

args_pairs(p::PromptedParser) = args_pairs(p.parser)
set_value!(p::PromptedParser, arg, value) = set_value!(p.parser, arg, value)
add_argument!(p::PromptedParser, arg_short, arg_long; kwargs...) = add_argument!(p.parser, arg_short, arg_long; kwargs...)
Expand All @@ -351,5 +351,4 @@ add_example!(p::PromptedParser, example) = add_example!(p.parser, example)
help(p::PromptedParser; color=p.color) = help(p.parser; color)
get_value(p::PromptedParser, arg) = get_value(p.parser, arg)


end # module SimpleArgParse

0 comments on commit 58d333c

Please sign in to comment.