# CommandParser
<span class="source-link">[[Source]](src/cli/command_parser.md#L3)</span>
```pony
class ref CommandParser
```

## Constructors

### create
<span class="source-link">[[Source]](src/cli/command_parser.md#L7)</span>


Creates a new parser for a given command spec.


```pony
new box create(
  spec': CommandSpec box)
: CommandParser box^
```
#### Parameters

*   spec': [CommandSpec](cli-CommandSpec) box

#### Returns

* [CommandParser](cli-CommandParser) box^

---

### _sub
<span class="source-link">[[Source]](src/cli/command_parser.md#L14)</span>


```pony
new box _sub(
  spec': CommandSpec box,
  parent': CommandParser box)
: CommandParser box^
```
#### Parameters

*   spec': [CommandSpec](cli-CommandSpec) box
*   parent': [CommandParser](cli-CommandParser) box

#### Returns

* [CommandParser](cli-CommandParser) box^

---

## Public Functions

### parse
<span class="source-link">[[Source]](src/cli/command_parser.md#L18)</span>


Parses all of the command line tokens and env vars and returns a Command,
or the first SyntaxError.


```pony
fun box parse(
  argv: Array[String val] box,
  envs: (Array[String val] box | None val) = seq)
: (Command box | CommandHelp box | SyntaxError val)
```
#### Parameters

*   argv: [Array](builtin-Array)\[[String](builtin-String) val\] box
*   envs: ([Array](builtin-Array)\[[String](builtin-String) val\] box | [None](builtin-None) val) = seq

#### Returns

* ([Command](cli-Command) box | [CommandHelp](cli-CommandHelp) box | [SyntaxError](cli-SyntaxError) val)

---

## Private Functions

### _fullname
<span class="source-link">[[Source]](src/cli/command_parser.md#L36)</span>


```pony
fun box _fullname()
: String val
```

#### Returns

* [String](builtin-String) val

---

### _root_spec
<span class="source-link">[[Source]](src/cli/command_parser.md#L43)</span>


```pony
fun box _root_spec()
: CommandSpec box
```

#### Returns

* [CommandSpec](cli-CommandSpec) box

---

### _parse_command
<span class="source-link">[[Source]](src/cli/command_parser.md#L50)</span>


Parses all of the command line tokens and env vars into the given options
and args maps. Returns the first SyntaxError, or the Command when OK.


```pony
fun box _parse_command(
  tokens: Array[String val] ref,
  options: HashMap[String val, Option val, HashEq[String val] val] ref,
  args: HashMap[String val, Arg val, HashEq[String val] val] ref,
  envsmap: HashMap[String val, String val, HashEq[String val] val] box,
  ostop: Bool val)
: (Command box | CommandHelp box | SyntaxError val)
```
#### Parameters

*   tokens: [Array](builtin-Array)\[[String](builtin-String) val\] ref
*   options: [HashMap](collections-HashMap)\[[String](builtin-String) val, [Option](cli-Option) val, [HashEq](collections-HashEq)\[[String](builtin-String) val\] val\] ref
*   args: [HashMap](collections-HashMap)\[[String](builtin-String) val, [Arg](cli-Arg) val, [HashEq](collections-HashEq)\[[String](builtin-String) val\] val\] ref
*   envsmap: [HashMap](collections-HashMap)\[[String](builtin-String) val, [String](builtin-String) val, [HashEq](collections-HashEq)\[[String](builtin-String) val\] val\] box
*   ostop: [Bool](builtin-Bool) val

#### Returns

* ([Command](cli-Command) box | [CommandHelp](cli-CommandHelp) box | [SyntaxError](cli-SyntaxError) val)

---

### _parse_long_option
<span class="source-link">[[Source]](src/cli/command_parser.md#L205)</span>


--opt=foo => --opt has argument foo
--opt foo => --opt has argument foo, iff arg is required


```pony
fun box _parse_long_option(
  token: String val,
  args: Array[String val] ref)
: (Option val | SyntaxError val)
```
#### Parameters

*   token: [String](builtin-String) val
*   args: [Array](builtin-Array)\[[String](builtin-String) val\] ref

#### Returns

* ([Option](cli-Option) val | [SyntaxError](cli-SyntaxError) val)

---

### _parse_short_options
<span class="source-link">[[Source]](src/cli/command_parser.md#L222)</span>


if 'O' requires an argument
  -OFoo  => -O has argument Foo
  -O=Foo => -O has argument Foo
  -O Foo => -O has argument Foo
else
  -O=Foo => -O has argument foo
-abc => options a, b, c.
-abcFoo => options a, b, c. c has argument Foo iff its arg is required.
-abc=Foo => options a, b, c. c has argument Foo.
-abc Foo => options a, b, c. c has argument Foo iff its arg is required.


```pony
fun box _parse_short_options(
  token: String val,
  args: Array[String val] ref)
: (Array[Option val] ref | SyntaxError val)
```
#### Parameters

*   token: [String](builtin-String) val
*   args: [Array](builtin-Array)\[[String](builtin-String) val\] ref

#### Returns

* ([Array](builtin-Array)\[[Option](cli-Option) val\] ref | [SyntaxError](cli-SyntaxError) val)

---

### _parse_arg
<span class="source-link">[[Source]](src/cli/command_parser.md#L272)</span>


```pony
fun box _parse_arg(
  token: String val,
  arg_pos: USize val)
: (Arg val | SyntaxError val)
```
#### Parameters

*   token: [String](builtin-String) val
*   arg_pos: [USize](builtin-USize) val

#### Returns

* ([Arg](cli-Arg) val | [SyntaxError](cli-SyntaxError) val)

---

### _option_with_name
<span class="source-link">[[Source]](src/cli/command_parser.md#L280)</span>


```pony
fun box _option_with_name(
  name: String val)
: (OptionSpec val | None val)
```
#### Parameters

*   name: [String](builtin-String) val

#### Returns

* ([OptionSpec](cli-OptionSpec) val | [None](builtin-None) val)

---

### _option_with_short
<span class="source-link">[[Source]](src/cli/command_parser.md#L290)</span>


```pony
fun box _option_with_short(
  short: U8 val)
: (OptionSpec val | None val)
```
#### Parameters

*   short: [U8](builtin-U8) val

#### Returns

* ([OptionSpec](cli-OptionSpec) val | [None](builtin-None) val)

---

### _short_string
<span class="source-link">[[Source]](src/cli/command_parser.md#L302)</span>


```pony
fun tag _short_string(
  c: U8 val)
: String val
```
#### Parameters

*   c: [U8](builtin-U8) val

#### Returns

* [String](builtin-String) val

---

### _help_name
<span class="source-link">[[Source]](src/cli/command_parser.md#L305)</span>


```pony
fun box _help_name()
: String val
```

#### Returns

* [String](builtin-String) val

---

