|
| 1 | +''' |
| 2 | +Python manatools.args contains an base for parsing argument passed from command line. |
| 3 | +
|
| 4 | +License: LGPLv2+ |
| 5 | +
|
| 6 | +Author: Papoteur |
| 7 | +
|
| 8 | +@package mamatools.args |
| 9 | +''' |
| 10 | +import argparse |
| 11 | +import gettext |
| 12 | + |
| 13 | +class AppArgs : |
| 14 | + ''' |
| 15 | + AppArs contains an base for parsing argument passed from command line. |
| 16 | + command is the application name |
| 17 | + It can be superseded to add specific options |
| 18 | + ''' |
| 19 | + def __init__(self, command) : |
| 20 | + |
| 21 | + # We need to call this as early as possible because |
| 22 | + # command-line help strings are translated |
| 23 | + gettext.install(command, localedir='/usr/share/locale', names=('ngettext',)) |
| 24 | + self.parser = argparse.ArgumentParser(prog=command, usage='%(prog)s [options]') |
| 25 | + ui_select_parser = self.parser.add_mutually_exclusive_group() |
| 26 | + # libyui pass through arguments |
| 27 | + ui_select_parser.add_argument('--gtk', help=_('start using yui GTK+ plugin implementation'), action='store_true') |
| 28 | + ui_select_parser.add_argument('--ncurses', help=_('start using yui ncurses plugin implementation'), action='store_true') |
| 29 | + ui_select_parser.add_argument('--qt', help=_('start using yui Qt plugin implementation'), action='store_true') |
| 30 | + self.parser.add_argument('--fullscreen', help=_('use full screen for dialogs'), action='store_true') |
| 31 | + |
| 32 | + # Application arguments |
| 33 | + self.parser.add_argument('--locales-dir', nargs='?', help=_('directory containing localization strings (developer only)')) |
| 34 | + self.parser.add_argument('--version', help=_('show application version and exit'), action='store_true') |
| 35 | + |
| 36 | + @property |
| 37 | + def args(self) : |
| 38 | + ''' |
| 39 | + returns args parsed from command line |
| 40 | + ''' |
| 41 | + return self.parser.parse_args() |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + # Change localedir if "--locales-dir" option is specified |
| 46 | + if args.locales_dir: |
| 47 | + gettext.install(command, localedir=args.locales_dir, names=('ngettext',)) |
0 commit comments