Skip to content

Commit

Permalink
Allow multiple authors to search for. Allow authors to exclude. Allow…
Browse files Browse the repository at this point in the history
… some command line params to be listed multiple times.
  • Loading branch information
Michael Tougeron committed Feb 6, 2011
1 parent a9bddc1 commit 5f10ff4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
48 changes: 41 additions & 7 deletions CliParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ class CliParser
*/
const TYPE_FLAG = 2;

/**
* @var int Indicates an input option can be provided as a
* key/value pair multiple times.
* (i.e., "--var=foo --var=bar" or "--var=foo,bar")
*/
const TYPE_ARRAY = 3;

/**
* @var string the delimiter to use with self::TYPE_ARRAY
*/
const DEFAULT_DELIMITER = ',';

/**
* Map of args provided on the command line, keyed by the short name.
*
Expand Down Expand Up @@ -68,26 +80,38 @@ public function about($msg)
/**
* Inform the parser of an option that your program accepts.
*
* @var string $short Single letter short name for the option.
* @var string $long Multi letter long name for the option.
* @var string $help Message to display in usage output.
* @var bool $required True if the user is required to set this option.
* @var int $type self::TYPE_VALUE or self::TYPE_FLAG
* @var string $short Single letter short name for the option.
* @var string $long Multi letter long name for the option.
* @var string $help Message to display in usage output.
* @var bool $required True if the user is required to set this option.
* @var int $type self::TYPE_VALUE or self::TYPE_FLAG or self::TYPE_ARRAY
* @var string $delimiter Delimiter to use if $type = self::TYPE_ARRAY
*
* @return Opt (supports fluent interface)
*/
public function addOpt($short, $long, $help, $required=false, $type=self::TYPE_VALUE)
public function addOpt(
$short,
$long,
$help,
$required = false,
$type = self::TYPE_VALUE,
$delimiter = self::DEFAULT_DELIMITER
)
{
$this->longNameMap[$long] = $short;
if ($this->maxNameLength == 0 || strlen($long) > $this->maxNameLength) {
$this->maxNameLength = strlen($long);
}
if ( $type == self::TYPE_ARRAY && empty($delimiter) ) {
$delimiter = self::DEFAULT_DELIMITER;
}
$this->spec[$short] = array(
'short' => $short,
'long' => $long,
'help' => $help,
'required' => $required,
'type' => $type);
'type' => $type,
'delimiter'=> $delimiter);
return $this;
}

Expand Down Expand Up @@ -119,6 +143,16 @@ public function load(array $argv)
$this->debug("load(): MATCHES ({$spec['short']}, {$spec['long']})");
if ($spec['type'] == self::TYPE_FLAG) {
$this->opts[$spec['short']] = true;
} elseif ($spec['type'] == self::TYPE_ARRAY) {
if ( strpos($value, $spec['delimiter']) !== false ) {
$value = explode($spec['delimiter'], $value);
$value = array_filter($value);
foreach ($value as $v) {
$this->opts[$spec['short']][] = $v;
}
} else {
$this->opts[$spec['short']][] = $value;
}
} else {
$this->opts[$spec['short']] = $value;
}
Expand Down
23 changes: 15 additions & 8 deletions Slog.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function __construct($repo, $days, $limit)
*
* @return Slog (supports fluent interface)
*/
public function setDebug($status=true)
public function setDebug($status = true)
{
$this->debug = (bool)$status;
return $this;
Expand Down Expand Up @@ -171,7 +171,11 @@ public function fetchXml($cmd)
public function removeCommitsFromAuthor($author)
{
if (!is_array($author)) {
$author = array($author);
$author = explode(',', $author);
}
$author = array_filter($author);
if (empty($author)) {
return;
}
foreach ($author as $a) {
$this->removeAuthors[$a] = 1;
Expand All @@ -187,12 +191,15 @@ public function removeCommitsFromAuthor($author)
*/
public function matchAuthor($author)
{
if (is_array($author)) {
foreach ($author as $a) {
$this->mustMatchAuthors[$a] = 1;
}
} else {
$this->mustMatchAuthors[$author] = 1;
if (!is_array($author)) {
$author = explode(',', $author);
}
$author = array_filter($author);
if (empty($author)) {
return;
}
foreach ($author as $a) {
$this->mustMatchAuthors[$a] = 1;
}
return $this;
}
Expand Down
8 changes: 7 additions & 1 deletion cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
. 'additional filtering capabilities such as '
. 'filtering by regex and author.')
->addOpt('a', 'author', 'Only show commits written by author. '
. 'Set this repeatedly to cast a larger net.')
. 'Set this repeatedly to cast a larger net.', false, CliParser::TYPE_ARRAY, ',')
->addOpt('c', 'color', 'Use color to style output.', false, CliParser::TYPE_FLAG)
->addOpt('d', 'days', 'Number of days of data to fetch.')
->addOpt('e', 'regex', "Only show commits that match regex. Set this "
. "repeatedly to cast a larger net.\n"
. "e.g., /component/")
->addOpt('f', 'format', 'Format type: summary (default), oneline')
->addOpt('h', 'help', 'Show usage.', false, CliParser::TYPE_FLAG)
->addOpt('i', 'ignore', 'Exclude commits written by author. '
. 'Set this repeatedly to cast a larger net.', false, CliParser::TYPE_ARRAY, ',')
->addOpt('l', 'limit', 'Limit the number of commits to search')
->addOpt('r', 'repo', "SVN repository, e.g., http://svn.host.com/project.\n"
. "If not specified but CWD is a working copy, the working "
Expand Down Expand Up @@ -65,9 +67,13 @@
if ($cli->get('verbose')) {
$slog->setDebug(true);
}

if ($cli->get('author')) {
$slog->matchAuthor($cli->get('author'));
}
if ($cli->get('ignore')) {
$slog->removeCommitsFromAuthor($cli->get('ignore'));
}
if ($cli->get('regex')) {
$slog->matchRegex($cli->get('regex'));
}
Expand Down

0 comments on commit 5f10ff4

Please sign in to comment.