Giter Site home page Giter Site logo

rue-ryuzaki / argparse Goto Github PK

View Code? Open in Web Editor NEW
13.0 13.0 3.0 2.54 MB

Argument parser for C++

Home Page: https://launchpad.net/~golubchikov-mihail/+archive/ubuntu/cpp-argparse

License: MIT License

CMake 1.04% C++ 98.96%
argparse argument-parser bash-completion command-line-parser cpp cpp-argparse cpp-argparse-dev cpp11 header-only

argparse's People

Contributors

arsenic-atg avatar lgtm-migrator avatar rue-ryuzaki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

argparse's Issues

GitHub actions: mingw job for Windows workflow

Current mingw job:

  mingw:
    runs-on: windows-latest
    strategy:
      fail-fast: false
      matrix:
        architecture: [x64, x86]
        standard: [98, 11, 14, 17, 20]

    steps:
    - uses: actions/checkout@v2

    - name: Set up MinGW
      uses: egor-tensin/setup-mingw@v2
      with:
        platform: ${{ matrix.architecture }}

    - name: Configure CMake
      run: cmake -B ${{github.workspace}}/build -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCMAKE_CXX_STANDARD=${{ matrix.standard }}

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

    - name: Test
      working-directory: ${{github.workspace}}/build
      run: ctest -C ${{env.BUILD_TYPE}}

often falls is Set up MinGW step with error:

Chocolatey v1.1.0
Upgrading the following packages:
mingw
By upgrading, you accept licenses for the packages.

mingw v8.1.0 [Approved]
mingw package files upgrade completed. Performing other installation steps.
Downloading mingw 64 bit
  from 'https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z/download'
ERROR: The remote file either doesn't exist, is unauthorized, or is forbidden for url 'https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z/download'. Exception calling "GetResponse" with "0" argument(s): "The request was aborted: Could not create SSL/TLS secure channel." 
This package is likely not broken for licensed users - see https://docs.chocolatey.org/en-us/features/private-cdn.
The upgrade of mingw was NOT successful.
Error while running 'C:\ProgramData\chocolatey\lib\mingw\tools\chocolateyinstall.ps1'.
 See log for details.

Chocolatey upgraded 0/1 packages. 1 packages failed.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

Failures
 - mingw (exited 404) - Error while running 'C:\ProgramData\chocolatey\lib\mingw\tools\chocolateyinstall.ps1'.
 See log for details.

Other configurations are required to build with mingw for Windows

Typos detected

These are the typos detected by codespell in the project

README.md:34: macroses ==> macros
README.md:35: setted ==> set
./include/argparse/argparse.hpp:85: compability ==> compatibility
./include/argparse/argparse.hpp:2499: missmatch ==> mismatch

๐Ÿ› Attribute names for optional arguments

Description

Hi there!

It's been a while, happy holidays ๐Ÿ‘‹

I recently came across an integration detail that does not conform to the specification.

It seems that it is not possible to use internal values for attribute names in calls like args.exists("long_option") instead of args.exists("--long-option") for an argument specified as --long-option.

Excerpt from https://docs.python.org/3/library/argparse.html#dest

For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string. If no long option strings were supplied, dest will be derived from the first short option string by stripping the initial - character. Any internal - characters will be converted to _ characters to make sure the string is a valid attribute name.

Reproduction steps

Make a call to a function that requires an attribute name for an optional compound argument using explicit and internal values

Expected vs. actual results

I expected the calls to accept this subtlety

Minimal code example

No response

Error messages

No response

Compiler and operating system

g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

Library version

1.6.5

Validation

unknown warning group '-Wimplicit-fallthrough='

I am getting this warning with clang 13.0.0 on macos

../libs/argparse/include/argparse/argparse.hpp:97:32: warning: unknown warning group '-Wimplicit-fallthrough=', ignored [-Wunknown-warning-option]
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough="
                               ^
1 warning generated.
% clang++ --version
Homebrew clang version 13.0.0
Target: x86_64-apple-darwin21.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

How to access parsed args from within a handle lambda?

Is there a pattern to access the parsed args from within a handle lambda? I thought something like this might work but it doesn't seem to.

#include <argparse/argparse.hpp>
#include <iostream>

int main(int argc, char* argv[]) {
    argparse::ArgumentParser::Namespace space;
    auto parser = argparse::ArgumentParser(argc, argv);
    auto& subparsers = parser.add_subparsers();
    auto& parser_a = subparsers.add_parser("a")
                         .handle([&]() {
                             std::cout << "bar: " << space.get<uint32_t>("bar") << std::endl;
                         });
    parser_a.add_argument("bar");
    space = parser.parse_args();
    return 0;
}
AttributeError: 'Namespace' object has no attribute 'bar'

๐Ÿ› arguments parsing should be reviewed

Hello!

Implicit values (from 80ab3cc ) do not work if they are followed by other positional arguments (or subparsers), the following value might have to be compared to next available arguments in order to assert if that's a custom value or another argument.

Given a parser with an implicit value optional argument --arg and a positional argument value:

./mybin --arg mycustomvalue 42
# works
./mybin --arg 42
# fails

The same behavior occurs in the context of subparsers.

Given a parser with an implicit value optional argument --arg and a subparser a:

./mybin --arg mycustomvalue a
# works
./mybin --arg a
# fails

Allow share dest storage

Description

First, thanks for awesome library. I use Python argparse a lot, then now I can use it in C++.

I have similar issue to #4, specially the second case.
The goal is use default value without any option and allow override it or disable it (or change same predefined).

./mybin
# a) default arg "a"

./mybin --no-arg
# b) where arg is disabled, set to "b" for better view

./mybin --arg x
# x) where custom arg is "x"

Python version.

p = argparse.ArgumentParser()
p.add_argument('--arg', default='a')
# _StoreAction(option_strings=['--arg'], dest='arg', nargs=None, const=None, default='a', type=None, choices=None, required=False, help=None, metavar=None)
p.add_argument('--no-arg', dest='arg', action='store_const', const='b')
# _StoreConstAction(option_strings=['--no-arg'], dest='arg', nargs=0, const='b', default=None, type=None, choices=None, required=False, help=None, metavar=None)

p.parse_args(''.split())
# Namespace(arg='a')

p.parse_args('--arg x'.split())
# Namespace(arg='x')

p.parse_args('--no-arg'.split())
# Namespace(arg='b')

I have a problem with dest. There is a conflict or second argument overrides the first.
Of course I can use an another name add add if to override dest in my code after parsing. Than it's not so important issue.
Just note Python differs.

Reproduction steps

Compile and run.

Expected vs. actual results

Actual results

For dest("--arg") I have:

argparse::ArgumentError: argument --no-arg: conflicting dest string: --arg

For dest("arg") I have:

./mybin 
(empty)  arg is ''

./mybin --no-arg
(exists) arg is 'b'

./mybin --arg x
(empty)  arg is ''

Expected results

In both cases.

./mybin 
(exists) arg is 'a'

./mybin --no-arg
(exists) arg is 'b'

./mybin --arg x
(exists)  arg is 'x'

Minimal code example

#include "argparse.hpp"

int main(int argc, char *argv[])
{
	auto parser = argparse::ArgumentParser(argc, argv);
	parser.add_argument("--arg").default_value("a");
	parser.add_argument("--no-arg").action("store_const").dest("arg").const_value("b");
	auto const args = parser.parse_args();
	std::cout << (args.exists("arg") ? "(exists)" : "(empty) ") << " arg is '" << args.get<std::string>("arg") << "'" << std::endl;
	return 0;
}

Error messages

argparse::ArgumentError: argument --no-arg: conflicting dest string: --arg

Compiler and operating system

c++ -o mybin aa.cc
Debian testing.

Library version

1.6.4

Validation

โœจ add support for implicit values

Hello there!

A very useful library ๐Ÿ‘

Do you plan to incorporate implicit value support in the near future?

These implicit values behave like flags and allow you to use a predefined value or specify a custom one on activation.

This is slightly different from a default value, since when the argument is not specified on the command line, no value (or a null value) is assigned to the argument.

./mybin
# where arg is disabled

./mybin --arg
# where arg is "mypredefinedvalue"

./mybin --arg mycustomvalue
# where arg is "mycustomvalue"

This is an alternative to having 2 arguments for this task using a default and a flag, such as:

./mybin --no-arg
# where arg is disabled

./mybin
# where arg is "mypredefinedvalue"

./mybin --arg mycustomvalue
# where arg is "mycustomvalue"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.