Skip to content

Commit 5f218a9

Browse files
author
Papoteur
committed
Add AppArgs class for parsing argument from command.
1 parent 9665e87 commit 5f218a9

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

manatools/args.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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',))

test/testAppArgs.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# vim: set fileencoding=utf-8 :
2+
# vim: set et ts=4 sw=4:
3+
4+
'''
5+
Test services
6+
7+
License: LGPLv2+
8+
9+
Author: Angelo Naselli <[email protected]>
10+
11+
@package manatools
12+
'''
13+
14+
import manatools.args as args
15+
16+
17+
if __name__ == '__main__':
18+
parser = args.AppArgs('command')
19+
if parser.args.version:
20+
print("v1")
21+
22+

0 commit comments

Comments
 (0)