or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

argument-parsing.mdcore-cli.mdhelp-documentation.mdindex.mdinteractive-mode.mdshell-completion.md

argument-parsing.mddocs/

0

# Argument Parsing Customization

1

2

Fire provides decorators and utilities for customizing how command-line arguments are parsed and converted, allowing fine-grained control over argument processing for specific parameters or functions.

3

4

## Capabilities

5

6

### Custom Parse Function Decorator

7

8

Decorator to set a custom parsing function for specific arguments or as the default parser for a function.

9

10

```python { .api }

11

def SetParseFn(fn, *arguments):

12

"""

13

Set custom parsing function for specific arguments.

14

15

Parameters:

16

- fn: Function to use for parsing arguments

17

- *arguments: Argument names to apply custom parser to (if none, sets default parser)

18

19

Returns:

20

Decorated function with Fire metadata for custom parsing

21

"""

22

```

23

24

### Multiple Parse Functions Decorator

25

26

Decorator to set multiple custom parsing functions for different arguments in a single declaration.

27

28

```python { .api }

29

def SetParseFns(*positional, **named):

30

"""

31

Set multiple custom parsing functions.

32

33

Parameters:

34

- *positional: Functions for positional arguments

35

- **named: Mapping of argument names to parsing functions

36

37

Returns:

38

Decorated function with Fire metadata for custom parsing

39

"""

40

```

41

42

### Metadata Access

43

44

Utilities for accessing Fire metadata attached to functions by the parsing decorators.

45

46

```python { .api }

47

def GetMetadata(fn):

48

"""

49

Get Fire metadata from a function.

50

51

Parameters:

52

- fn: Function to get metadata from

53

54

Returns:

55

Dictionary containing Fire metadata

56

"""

57

58

def GetParseFns(fn):

59

"""

60

Get custom parse functions from a function.

61

62

Parameters:

63

- fn: Function to get parse functions from

64

65

Returns:

66

Dictionary containing parse functions configuration

67

"""

68

```

69

70

## Usage Examples

71

72

**Custom argument parser:**

73

```python

74

import fire

75

from fire.decorators import SetParseFn

76

77

def json_parser(value):

78

import json

79

return json.loads(value)

80

81

@SetParseFn(json_parser, 'config')

82

def process_data(data, config):

83

# config will be parsed as JSON

84

print(f"Processing {data} with config: {config}")

85

return "Done"

86

87

if __name__ == '__main__':

88

fire.Fire(process_data)

89

# Command line: python script.py mydata '{"option": "value"}'

90

```

91

92

**Multiple custom parsers:**

93

```python

94

import fire

95

from fire.decorators import SetParseFns

96

97

def parse_list(value):

98

return value.split(',')

99

100

def parse_float(value):

101

return float(value)

102

103

@SetParseFns(items=parse_list, threshold=parse_float)

104

def analyze(items, threshold):

105

filtered = [item for item in items if len(item) > threshold]

106

return filtered

107

108

if __name__ == '__main__':

109

fire.Fire(analyze)

110

# Command line: python script.py --items=apple,banana,cherry --threshold=5.5

111

```

112

113

**Default parser for all arguments:**

114

```python

115

import fire

116

from fire.decorators import SetParseFn

117

118

def always_upper(value):

119

return str(value).upper()

120

121

@SetParseFn(always_upper) # No argument names = default for all

122

def shout(message, prefix="ALERT"):

123

return f"{prefix}: {message}"

124

125

if __name__ == '__main__':

126

fire.Fire(shout)

127

# Command line: python script.py hello --prefix=warning

128

# Output: WARNING: HELLO

129

```

130

131

## Parser Function Requirements

132

133

Custom parser functions should:

134

135

1. **Accept a single string argument** - the raw command-line value

136

2. **Return the parsed value** - the converted Python object

137

3. **Handle errors gracefully** - raise appropriate exceptions for invalid input

138

4. **Be deterministic** - same input should produce same output

139

140

Example parser structure:

141

```python

142

def custom_parser(value):

143

try:

144

# Parse and convert the string value

145

result = some_conversion(value)

146

return result

147

except (ValueError, TypeError) as e:

148

raise ValueError(f"Invalid value '{value}': {e}")

149

```

150

151

## Metadata Constants

152

153

```python { .api }

154

FIRE_METADATA = 'FIRE_METADATA'

155

FIRE_PARSE_FNS = 'FIRE_PARSE_FNS'

156

ACCEPTS_POSITIONAL_ARGS = 'ACCEPTS_POSITIONAL_ARGS'

157

```

158

159

These constants are used internally by Fire to store and retrieve metadata from decorated functions.