1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

# vim: set fileencoding=utf-8 : 

# 

# (C) 2011 Guido Guenther <agx@sigxcpu.org> 

#    This program is free software; you can redistribute it and/or modify 

#    it under the terms of the GNU General Public License as published by 

#    the Free Software Foundation; either version 2 of the License, or 

#    (at your option) any later version. 

# 

#    This program is distributed in the hope that it will be useful, 

#    but WITHOUT ANY WARRANTY; without even the implied warranty of 

#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#    GNU General Public License for more details. 

# 

#    You should have received a copy of the GNU General Public License 

#    along with this program; if not, write to the Free Software 

#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

 

import collections 

 

class GitArgs(object): 

    """ 

    Handle arguments to git commands 

 

    >>> GitArgs('-h', '--no-foo').args 

    ['-h', '--no-foo'] 

    >>> GitArgs().add('--more-foo', '--less-bar').args 

    ['--more-foo', '--less-bar'] 

    >>> GitArgs().add(['--foo', '--bar']).args 

    ['--foo', '--bar'] 

    >>> GitArgs().add_cond(1 > 2, '--opt', '--no-opt').args 

    ['--no-opt'] 

    >>> GitArgs().add_true(True, '--true').args 

    ['--true'] 

    >>> GitArgs().add_false(True, '--true').args 

    [] 

    >>> GitArgs().add_false(False, '--false').args 

    ['--false'] 

    """ 

 

    def __init__(self, *args): 

        self._args = list(args) 

 

    @property 

    def args(self): 

        return self._args 

 

    def add(self, *args): 

        """ 

        Add arguments to argument list 

        """ 

        for arg in args: 

            if isinstance(arg, basestring): 

                self._args.append(arg) 

            elif isinstance(arg, collections.Iterable): 

                for i in iter(arg): 

                    self._args.append(str(i)) 

            else: 

                self._args.append(str(arg)) 

 

        return self 

 

    def add_true(self, condition, *args): 

        """ 

        Add I{args} if I{condition} is C{True} 

 

        @param condition: the condition to test 

        @type condition: C{bool} 

        @param args: arguments to add 

        """ 

        if condition: 

            self._args += list(args) 

        return self 

 

    def add_false(self, condition, *args): 

        """ 

        Add I{args} if I{condition} is C{False} 

 

        @param condition: the condition to test 

        @type condition: C{bool} 

        @param args: arguments to add 

        """ 

        self.add_true(not condition, *args) 

        return self 

 

    def add_cond(self, condition, opt, noopt=[]): 

        """ 

        Add option I{opt} to I{alist} if I{condition} is C{True} 

        else add I{noopt}. 

 

        @param condition: condition 

        @type condition: C{bool} 

        @param opt: option to add if I{condition} is C{True} 

        @type opt: C{str} or C{list} 

        @param noopt: option to add if I{condition} is C{False} 

        @type noopt: C{str} or C{list} 

        """ 

        if isinstance(opt, basestring): 

            opt = [ opt ] 

        if isinstance(noopt, basestring): 

            noopt = [ noopt ] 

        self._args += opt if condition else noopt 

        return self