Changeset View
Changeset View
Standalone View
Standalone View
src/parser/argument/PhutilArgumentParser.php
Show First 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | final class PhutilArgumentParser extends Phobject { | ||||
private $argv; | private $argv; | ||||
private $specs = array(); | private $specs = array(); | ||||
private $results = array(); | private $results = array(); | ||||
private $parsed; | private $parsed; | ||||
private $tagline; | private $tagline; | ||||
private $synopsis; | private $synopsis; | ||||
private $workflows; | private $workflows; | ||||
private $helpWorkflows; | |||||
private $showHelp; | private $showHelp; | ||||
private $requireArgumentTerminator = false; | private $requireArgumentTerminator = false; | ||||
private $sawTerminator = false; | private $sawTerminator = false; | ||||
const PARSE_ERROR_CODE = 77; | const PARSE_ERROR_CODE = 77; | ||||
private static $traceModeEnabled = false; | private static $traceModeEnabled = false; | ||||
▲ Show 20 Lines • Show All 356 Lines • ▼ Show 20 Lines | if (empty($this->workflows[$flow])) { | ||||
"%s\n", | "%s\n", | ||||
pht( | pht( | ||||
'(Assuming "%s" is the British spelling of "%s".)', | '(Assuming "%s" is the British spelling of "%s".)', | ||||
$flow, | $flow, | ||||
$corrected))); | $corrected))); | ||||
$flow = $corrected; | $flow = $corrected; | ||||
} else { | } else { | ||||
if (!$this->showHelp) { | |||||
$this->raiseUnknownWorkflow($flow, $corrected); | $this->raiseUnknownWorkflow($flow, $corrected); | ||||
} | } | ||||
} | } | ||||
} | |||||
$workflow = $this->workflows[$flow]; | $workflow = idx($this->workflows, $flow); | ||||
if ($this->showHelp) { | if ($this->showHelp) { | ||||
// Make "cmd flow --help" behave like "cmd help flow", not "cmd help". | // Make "cmd flow --help" behave like "cmd help flow", not "cmd help". | ||||
$help_flow = idx($this->workflows, 'help'); | $help_flow = idx($this->workflows, 'help'); | ||||
if ($help_flow) { | if ($help_flow) { | ||||
if ($help_flow !== $workflow) { | if ($help_flow !== $workflow) { | ||||
$workflow = $help_flow; | $workflow = $help_flow; | ||||
$argv = array($flow); | $argv = array($flow); | ||||
// Prevent parse() from dumping us back out to standard help. | // Prevent parse() from dumping us back out to standard help. | ||||
$this->showHelp = false; | $this->showHelp = false; | ||||
} | } | ||||
} else { | } else { | ||||
$this->printHelpAndExit(); | $this->printHelpAndExit(); | ||||
} | } | ||||
} | } | ||||
if (!$workflow) { | |||||
$this->raiseUnknownWorkflow($flow, $corrected); | |||||
} | |||||
$this->argv = array_values($argv); | $this->argv = array_values($argv); | ||||
if ($workflow->shouldParsePartial()) { | if ($workflow->shouldParsePartial()) { | ||||
$this->parsePartial($workflow->getArguments()); | $this->parsePartial($workflow->getArguments()); | ||||
} else { | } else { | ||||
$this->parse($workflow->getArguments()); | $this->parse($workflow->getArguments()); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | /* -( Reading Arguments )-------------------------------------------------- */ | ||||
} | } | ||||
public function setWorkflows($workflows) { | public function setWorkflows($workflows) { | ||||
$workflows = mpull($workflows, null, 'getName'); | $workflows = mpull($workflows, null, 'getName'); | ||||
$this->workflows = $workflows; | $this->workflows = $workflows; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function setHelpWorkflows(array $help_workflows) { | |||||
$help_workflows = mpull($help_workflows, null, 'getName'); | |||||
$this->helpWorkflows = $help_workflows; | |||||
return $this; | |||||
} | |||||
public function getWorkflows() { | public function getWorkflows() { | ||||
return $this->workflows; | return $this->workflows; | ||||
} | } | ||||
/* -( Command Help )------------------------------------------------------- */ | /* -( Command Help )------------------------------------------------------- */ | ||||
public function setRequireArgumentTerminator($require) { | public function setRequireArgumentTerminator($require) { | ||||
Show All 35 Lines | public function renderHelp() { | ||||
} | } | ||||
if ($this->synopsis) { | if ($this->synopsis) { | ||||
$out[] = $this->format('**%s**', pht('SYNOPSIS')); | $out[] = $this->format('**%s**', pht('SYNOPSIS')); | ||||
$out[] = $this->indent(6, $this->synopsis); | $out[] = $this->indent(6, $this->synopsis); | ||||
$out[] = null; | $out[] = null; | ||||
} | } | ||||
if ($this->workflows) { | $workflows = $this->helpWorkflows; | ||||
if ($workflows === null) { | |||||
$workflows = $this->workflows; | |||||
} | |||||
if ($workflows) { | |||||
$has_help = false; | $has_help = false; | ||||
$out[] = $this->format('**%s**', pht('WORKFLOWS')); | $out[] = $this->format('**%s**', pht('WORKFLOWS')); | ||||
$out[] = null; | $out[] = null; | ||||
$flows = $this->workflows; | $flows = $workflows; | ||||
ksort($flows); | ksort($flows); | ||||
foreach ($flows as $workflow) { | foreach ($flows as $workflow) { | ||||
if ($workflow->getName() == 'help') { | if ($workflow->getName() == 'help') { | ||||
$has_help = true; | $has_help = true; | ||||
} | } | ||||
$out[] = $this->renderWorkflowHelp( | $out[] = $this->renderWorkflowHelp( | ||||
$workflow->getName(), | $workflow->getName(), | ||||
$show_details = false); | $show_details = false); | ||||
Show All 34 Lines | /* -( Command Help )------------------------------------------------------- */ | ||||
public function renderWorkflowHelp( | public function renderWorkflowHelp( | ||||
$workflow_name, | $workflow_name, | ||||
$show_details = false) { | $show_details = false) { | ||||
$out = array(); | $out = array(); | ||||
$indent = ($show_details ? 0 : 6); | $indent = ($show_details ? 0 : 6); | ||||
$workflow = idx($this->workflows, strtolower($workflow_name)); | $workflows = $this->helpWorkflows; | ||||
if ($workflows === null) { | |||||
$workflows = $this->workflows; | |||||
} | |||||
$workflow = idx($workflows, strtolower($workflow_name)); | |||||
if (!$workflow) { | if (!$workflow) { | ||||
$out[] = $this->indent( | $out[] = $this->indent( | ||||
$indent, | $indent, | ||||
pht('There is no **%s** workflow.', $workflow_name)); | pht('There is no **%s** workflow.', $workflow_name)); | ||||
} else { | } else { | ||||
$out[] = $this->indent($indent, $workflow->getExamples()); | $out[] = $this->indent($indent, $workflow->getExamples()); | ||||
$out[] = $this->indent($indent, $workflow->getSynopsis()); | $out[] = $this->indent($indent, $workflow->getSynopsis()); | ||||
if ($show_details) { | if ($show_details) { | ||||
▲ Show 20 Lines • Show All 239 Lines • Show Last 20 Lines |