aboutsummaryrefslogtreecommitdiffhomepage
path: root/gbp/git/args.py
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2011-11-21 22:42:08 +0100
committerGuido Günther <agx@sigxcpu.org>2011-11-21 23:19:26 +0100
commiteabca1fcdc7cd932049608cbab634dd0ed63542e (patch)
treeadbdd97623fc852cdd20a49c7c757890fd7b98c1 /gbp/git/args.py
parent2881b9bc1555d5d9fa45aed390a4204704a073b2 (diff)
Add class to simplify git argument handling
Diffstat (limited to 'gbp/git/args.py')
-rw-r--r--gbp/git/args.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/gbp/git/args.py b/gbp/git/args.py
new file mode 100644
index 00000000..ff9077ea
--- /dev/null
+++ b/gbp/git/args.py
@@ -0,0 +1,91 @@
+# 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
+
+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_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
+ """
+ self._args += list(args)
+ 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
+