from hiargparse import ArgsProvider, Arg, ChildProvider, ArgumentParser

child = ArgsProvider(args=[Arg('baz', default=42)])
root = ArgsProvider(
    args=[Arg('foo', default='bar')],
    child_providers=[ChildProvider(provider=child, name='child')])
parser = ArgumentParser()
parser.add_arguments_from_provider(root)
print(parser.parse_args())
Beispiel #2
0
        back_tire_params._update({'radius': params.tire_radius + 1.0})
        # of course you can use normal access to the attribute
        back_tire_params.value *= 2
        self._back_tires = [Tire(back_tire_params) for i in range(2)]

    def print_spec(self) -> None:
        print('Car: ')
        print('front tires: {}'.format(', '.join([str(tire) for tire in self._front_tires])))
        print('back tires: {}'.format(', '.join([str(tire) for tire in self._back_tires])))
        if self._numbers is not None:
            print('by the way, I like {} hahaha'.format(', '.join([str(n) for n in self._numbers])))


if __name__ == '__main__':
    # set a root argument provider (same as other argument providers)
    args_provider = ArgsProvider(
        child_providers=[ChildProvider(Car)]
    )

    # quite usual argparse way except *new code* line
    parser = ArgumentParser()
    parser.add_argument('-V', '--version', action='version', version='v1.0')
    args_provider.add_arguments_to_parser(parser)  # *new code*
    params = parser.parse_args()

    # now you have ALL parameters including child and grandchild arguments
    print(params)

    car = Car(params.Car)
    car.print_spec()
                type=Path,
                help=
                '%(default-text)s Write a configure file in the given path and exit. '
                ),
            # read-from argument
            Arg('read-from',
                type=Path,
                help='%(default-text)s Read from the given configure file. '),
        ],
        child_providers=[ChildProvider(Son)])

    # parse arguments as usual
    parser = ArgumentParser()
    parser.add_argument('-V', '--version', action='version', version='v1.0')
    parser.add_arguments_from_provider(args_provider)
    params = parser.parse_args()

    # write to / read from a file
    file_type = ConfigureFileType[params.file_type]
    if params.write_to is not None:
        path_w: Path = params.write_to
        # write configure arguments to the given file as the given type
        with path_w.open('w') as f:
            f.write(
                args_provider.write_out_configure_arguments(
                    file_type.get_writer()))
        # When you want to write out a configure file,
        # usually you want to stop this program, fill in
        # your brand-new configure file, and then restart it.
        # so I'll exit
        exit()