Commit Graph

201 Commits

Author SHA1 Message Date
Ethan Slattery
89b9e22f9a add pre and post text options
add variables, assignment functions, and printing for text before and after the argument lists.
2020-01-02 14:15:44 -08:00
Zhihao Yuan
7c57e1e852
Record used names with copies of string_view 2020-01-02 02:19:09 -06:00
Zhihao Yuan
03a0ce831d
Simplify Argument exposition-only constructors
Although they are public, they are not meant for end-users
to use, so we can change its interface to use less sfinae.
2020-01-02 02:19:08 -06:00
Zhihao Yuan
e6c6c9b31c
Get arguments in optional<T> with .present<T>()
fixes: p-ranav/argparse#66
2019-12-01 02:14:13 -06:00
Zhihao Yuan
e8a44d289d
Parse floating-point numbers in .scan
fixes: p-ranav/argparse#63
2019-11-26 00:11:21 -06:00
Zhihao Yuan
426a5dbb76
Parse integers in the .scan fluent interface 2019-11-25 23:45:55 -06:00
Zhihao Yuan
ea2f16d264
Upgrade and describe supported toolchains 2019-11-25 17:23:28 -06:00
Zhihao Yuan
964790cf3c
Determine negative numeric values with a grammar
Two differences that diverge from the existing behavior:

  1. Leading zeros are not allowed for integers.  Negative
     octal numbers such as `-066` are not meant to be treated
     as positional arguments, but existing code recognize them
     as decimal numbers.  Note that negative floating-point
     numbers with leading zeros (`-003.`) are unambiguous and
     are recognized.
  2. Inf and NaN are not recognized.  This is because options
     like `-inf` is indistinguishable from a compound argument
     that meant to be a shorthand for `-i -n -f`.

fixes: p-ranav/argparse#55
2019-11-24 00:31:05 -06:00
Zhihao Yuan
77f3bd9b8d
Default --help to print help and exit
The change also fixes a rare bug introduced in 9007958:
when there are duplicated keys, insert_or_update overrides
previously inserted ones, emplace doesn't.

fixes: p-ranav/argparse#59
2019-11-22 16:54:58 -06:00
Zhihao Yuan
36003197c7
Fix print_help for printing nothing 2019-11-22 15:33:15 -06:00
Pranav Srinivas Kumar
c707bcc1c4 Closes #56 2019-11-21 10:25:41 -06:00
Zhihao Yuan
991df83d97
Support capturing remaining() arguments
This kind of argument works as if having the "remaining" nargs,
inspired by Python's `argparse.REMAINDER`.

This change also reduces the size of `Argument` by 8 bytes.

See also: https://docs.python.org/2/library/argparse.html#nargs

fixes: p-ranav/argparse#17
2019-11-20 17:47:20 -06:00
Zhihao Yuan
f7dae0d93d
Refactor some uses of SFINAE into constexpr-if 2019-11-17 20:17:27 -06:00
Zhihao Yuan
93ddf85a7b
Factor out any_cast_container 2019-11-17 20:05:25 -06:00
Zhihao Yuan
3177f544f5
Default value is not a container of std::any 2019-11-17 19:11:09 -06:00
Zhihao Yuan
4c5ded8c84
Constrain Argument constructor better
Without this change, std::allocator can construct Argument
2019-11-17 18:55:52 -06:00
Zhihao Yuan
556d935491
Suppress MSVC warnings and a Codacy warning 2019-11-17 13:42:28 -06:00
Zhihao Yuan
8201a18568
Fix various issues in Argument constructor
Before this change:

1. When the input is built-in string literal or cv-`char*`,
   `is_optional` constructs temporary `std::string` while
   `mNames` initializer is also constructing `std::string`
   due to the use of `std::initializer_list`.
2. When the input is `std::string_view`, doesn't compile.
3. When the input is `std::string`, `mNames` initializer
   moves `args`.  If argument name is longer than
   `std::string`'s SSO buffer, bad thing will happen because
   `is_optional` will be accessing `args` in moved-from
   states.

Because of the use of `strtol` which expects nul-terminated
input, `is_*` series functions must take `std::string`.  This
restriction may be removed after AppleClang adds `<charconv>`.
But for now, it complicates the patch.  My solution is to
create an array prvalue still, but use a array reference
rather than `std::initializer_list` to refer to it, so that
the code in delegated constructor can keep using fold
expressions after the necessary `std::string` objects being
created.
2019-11-17 01:51:36 -06:00
Zhihao Yuan
7dd6655a9e
Avoid extra copy made by initializer_list 2019-11-17 01:51:36 -06:00
Zhihao Yuan
56c041707a
Use string_view in getter interface 2019-11-17 01:51:31 -06:00
Zhihao Yuan
f7fe9cf439
Remove unused space in Argument structure 2019-11-16 21:51:01 -06:00
Zhihao Yuan
daeca099e2
Remove undesired access control 2019-11-16 21:45:18 -06:00
Zhihao Yuan
955e1e1e6c
Simplify code with four-legged std::equal 2019-11-16 21:26:49 -06:00
Zhihao Yuan
50c91a0bcc
Clang-format the code 2019-11-16 21:17:53 -06:00
Zhihao Yuan
f84f17d719
Give ArgumentParser value semantics
fixes: p-ranav/argparse#50
2019-11-16 15:15:54 -06:00
Zhihao Yuan
9007958c1f
Index arguments without storing their names twice 2019-11-16 14:35:35 -06:00
Zhihao Yuan
3f949fc3f1
Stop sharing argument values with parent parsers
See also: p-ranav/argparse#50
2019-11-16 14:33:00 -06:00
Zhihao Yuan
2208ec5b2d
Remove unused copy of parent parsers 2019-11-16 01:51:47 -06:00
Zhihao Yuan
f6e686b69c
Bind extra arguments to actions
closes: p-ranav/argparse#38
2019-11-14 01:30:04 -06:00
Zhihao Yuan
dc227448f6
Allow actions that return void
closes: p-ranav/argparse#39
2019-11-13 12:54:07 -06:00
Pranav Srinivas Kumar
bda0866320
Merge pull request #44 from lichray/help-stream
Help stream
2019-11-13 08:36:58 -06:00
Zhihao Yuan
c6cc306ec8
Remove the undocumented PARSE_ARGS macro
closes: p-ranav/argparse#41
2019-11-13 03:00:42 -06:00
Zhihao Yuan
dc74051832
Deprecate print_help()
closes: p-ranav/argparse#40
2019-11-13 02:40:15 -06:00
Zhihao Yuan
9d66976421
Print ArgumentParser help with stream insertion 2019-11-13 02:17:28 -06:00
mu001999
dd528a1d70 Add name for anonymous namespace 2019-09-01 21:16:59 +08:00
Pranav Srinivas Kumar
a9bc1c9a4a Closes #28 2019-08-17 17:56:29 -05:00
Pranav Srinivas Kumar
cb04248cfa Updated formatting 2019-08-17 17:01:04 -05:00
Pranav Srinivas Kumar
aba9b10101
Merge pull request #32 from wtdcode/remove-newlines
Remove extra new lines
2019-08-06 08:49:01 -05:00
Mio
4712288086 Change the format of required arguments 2019-08-06 20:52:31 +08:00
Mio
39c720e6c0 Add a mIsRquired field 2019-08-06 20:48:51 +08:00
Mio
7cb710f404 Remove extra new lines 2019-08-06 20:41:24 +08:00
Pranav Srinivas Kumar
73f6aa7538 Fixes Issue #24 2019-06-06 21:24:32 -04:00
Pranav Srinivas Kumar
af650392e7 Fixes Issue #22 - Tested w/ Visual Studio 2019 2019-06-05 09:13:51 -04:00
Stephan van Veen
6d46876f5c Do some cleanup in print_help 2019-05-25 20:07:24 +02:00
Stephan van Veen
67e535e171 Move print_help logic into Argument 2019-05-25 20:01:04 +02:00
Stephan van Veen
34d259d892 Sort names on argument construction 2019-05-25 09:49:00 +02:00
Stephan van Veen
d960a41e96 Use local iterator instead of member counter 2019-05-20 22:21:57 +02:00
Stephan van Veen
f2e0bd0de1 Iterate over map instead of both lists 2019-05-20 22:21:57 +02:00
Stephan van Veen
62c2be634a Remove argc argv version of parse_args_internal 2019-05-20 22:21:57 +02:00
Stephan van Veen
9e7b80034e Throw exception in case of unknown argument 2019-05-20 22:21:57 +02:00
Stephan van Veen
ecf8e4fd5b Implement parse_args_internal for compound parameters 2019-05-20 22:20:07 +02:00
Stephan van Veen
d95f9d9f14 First check for positional, then optional and compound 2019-05-20 22:20:07 +02:00
Stephan van Veen
44bef34e79 Implement parse_args_internal for positional parameters 2019-05-20 22:20:07 +02:00
Stephan van Veen
3c9a74049f Implement parse_args_internal for optional parameters 2019-05-20 22:20:07 +02:00
Stephan van Veen
4da8454a5a Simplify is_optional check 2019-05-18 14:06:46 +02:00
Pranav Srinivas Kumar
c03e34f981
Merge pull request #18 from svanveen/fix/container-types
Unify Argument::get and Argument::operator== for container types
2019-05-14 19:59:31 -04:00
Stephan van Veen
b3494a29d6 Remove is_specialization 2019-05-13 22:53:47 +02:00
Stephan van Veen
20a7d90abe Don't accept std::string as container 2019-05-13 22:52:26 +02:00
Stephan van Veen
a6ceffdb63 Extend doxygen documentation 2019-05-13 22:42:37 +02:00
Stephan van Veen
f08a280f92 Throw std::logic_error instead of returning empty value 2019-05-13 22:42:37 +02:00
Stephan van Veen
02b3ed1878 Cleanup Argument::get methods 2019-05-13 22:42:37 +02:00
Stephan van Veen
ca68260ec4 Enable equality operator for all iterable types 2019-05-13 22:42:36 +02:00
Pranav Srinivas Kumar
bd8d720faa
Merge pull request #16 from Jackojc/fix_const_argv
Allow use of both const char** and char** for argv.
2019-05-12 14:59:12 -04:00
Jack Clarke
3e1a42e312 allow use of const char** and char** for argv 2019-05-12 18:40:30 +01:00
Jack Clarke
5e1b85fdc5 fix warning for implicit type conversion 2019-05-12 17:38:18 +01:00
Stephan van Veen
94ca8e2552 Unify container operations 2019-05-12 17:17:21 +02:00
Stephan van Veen
15f45c2054 Simplify get method 2019-05-12 15:21:11 +02:00
Stephan van Veen
84adf5b2df Simplify creating of help option 2019-05-12 14:55:45 +02:00
Stephan van Veen
ecf8286c9e Resolve template recursion in add_parents method 2019-05-12 14:55:45 +02:00
Stephan van Veen
3bd7b342f0 Add const 2019-05-12 14:55:45 +02:00
Stephan van Veen
e923cf2ac3 Invert if-condition to improve readability 2019-05-12 14:55:45 +02:00
Stephan van Veen
95746ae159 Put helper methods into anonymous namespace 2019-05-12 14:55:44 +02:00
Stephan van Veen
8dd508d4b6 Put error message into exception instead of std::cerr 2019-05-12 14:55:44 +02:00
Stephan van Veen
603e87ae69 Move validation logic into Argument class itself 2019-05-12 14:55:44 +02:00
Stephan van Veen
7c938b1f2b Use stl algorithms instead of nested for loops 2019-05-12 14:55:44 +02:00
Stephan van Veen
a364f3f1e7 Remove code duplication 2019-05-11 18:46:55 +02:00
Stephan van Veen
12ad27f9c3 Don't call print_help inside ArgumentParser 2019-05-11 13:11:49 +02:00
Stephan van Veen
42ff186743 Put error message into exception instead of printing 2019-05-11 12:51:56 +02:00
Stephan van Veen
3c65c5dcab Add MACRO which calls exit(0) on any runtime_error in parse_args 2019-05-11 11:24:41 +02:00
Stephan van Veen
a0fa02503e Throw std::runtime_error instead of calling exit(0) 2019-05-11 11:23:44 +02:00
Stephan van Veen
94e4458641 Make use of move semantics 2019-05-11 00:58:20 +02:00
Stephan van Veen
b1c566b434 Prefer emplace_back over push_back 2019-05-10 19:11:43 +02:00
Stephan van Veen
6d3be1927c Use new constructor of Argument in constructor of ArgumentParser 2019-05-10 17:20:12 +02:00
Stephan van Veen
10fab58969 Replace upsert method by std::map::insert_or_assign 2019-05-10 17:06:28 +02:00
Stephan van Veen
91cd2477de Fix broken indentation 2019-05-09 20:31:10 +02:00
Stephan van Veen
2dc9121acf Simplify type traits 2019-05-09 18:44:47 +02:00
Stephan van Veen
df21d07bd6 Prefer emplace_back over push_back 2019-05-09 18:44:46 +02:00
Stephan van Veen
bef90a8d4f Make use of move semantics 2019-05-09 18:44:46 +02:00
Stephan van Veen
1eadf94438 Use empty() instead of size comparison 2019-05-09 18:44:46 +02:00
Stephan van Veen
d57b191f77 Use range based for loop 2019-05-09 18:44:46 +02:00
Stephan van Veen
89e4bb11ac Use auto type for iterators 2019-05-09 18:44:46 +02:00
Stephan van Veen
328f1048da Initialize mNames using 2019-05-09 18:44:46 +02:00
Stephan van Veen
d9bff3dc2b Construct Argument using template pack of names 2019-05-09 18:44:46 +02:00
Stephan van Veen
8474147a7c Use default constructor and non-static member initializers 2019-05-09 18:44:46 +02:00
Jack Clarke
4448983ecf fixed warnings 2019-05-07 17:04:48 +01:00
Pranav Srinivas Kumar
3ea4c79137 Fixes core dump on optional arg (issue #10) 2019-05-04 11:49:30 -04:00
Pranav Srinivas Kumar
7c9f83c7e1 Fixes issue #9 where error message shows the small form of the argument even though the user enters the long version 2019-05-01 20:06:13 -04:00
Pranav Srinivas Kumar
4d98282e2f Fixed formatting in error message (Issue #9) 2019-05-01 19:55:40 -04:00
Zhihao Yuan
af7380f98b
Fix C4267 in MSVC 2019-04-23 18:28:15 -05:00
Pranav Srinivas Kumar
88eca55b06 Fixes issue #6 2019-04-18 22:42:43 -04:00
Pranav Srinivas Kumar
bb4a2dbba7 Renamed directories 2019-04-01 22:01:40 -04:00