app/argparse: expect no value for a boolean option
Before commit 03f85d4c ('app: fix boolean handling in argparse module') the module does not expect a value after a 'boolean' argument. However there was the problem: a 'boolean' argument can be passed only at end of an argument list, otherwise it wrongly consumes a next argument and gives a confusing error message. The mentioned commit fixes this behaviour in the following way: it still allows to pass a 'boolean' argument at end of the list w/o a value, but requires a value ('true', 'false', '1', '0') if a 'boolean' argument is not at the end to be provided using {'--foo=true'} or {'--foo', 'true'} syntax. Here this behaviour is changed: a 'boolean' argument does not assume an explicitly passed value despite its position in an argument list. If a 'boolean' argument appears in the list, then argparse.parse() returns `true` for its value (a list of `true` values in case of 'boolean+' argument), otherwise it will not be added to the result. This change also makes the behaviour of long (--foo) and short (-f) 'boolean' options consistent. The motivation of the change is simple: it is easier and more natural to type, say, `tarantoolctl cat --show-system 00000000000000000000.snap` then `tarantoolctl cat --show-system true 00000000000000000000.snap`. This commit adds several new test cases, but it does not mean that we guarantee that the module behaviour will not be changed around some corner cases, say, handling of 'boolean+' arguments. This is internal module. Follows up #4076. Reviewed-by:Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Showing
- src/lua/argparse.lua 45 additions, 23 deletionssrc/lua/argparse.lua
- test/app/argparse.result 4 additions, 4 deletionstest/app/argparse.result
- test/app/gh-4076-argparse-wrong-bool-handling.result 131 additions, 12 deletionstest/app/gh-4076-argparse-wrong-bool-handling.result
- test/app/gh-4076-argparse-wrong-bool-handling.test.lua 55 additions, 5 deletionstest/app/gh-4076-argparse-wrong-bool-handling.test.lua
Loading
Please register or sign in to comment