Skip to content

positional

handle_field(name, field)

Treat the field as a positional field.

Parameters:

Name Type Description Default
name str

The name of the argument.

required
field FieldInfo

A dictionary representation of the field.

required

Returns:

Type Description
FuncArgs

Function arguments for the parser add_argument method.

Source code in m/cli/engine/parsers/positional.py
def handle_field(name: str, field: FieldInfo) -> FuncArgs:
    """Treat the field as a positional field.

    Args:
        name: The name of the argument.
        field: A dictionary representation of the field.

    Returns:
        Function arguments for the parser `add_argument` method.
    """
    extras = cast(dict, field.json_schema_extra)
    default = field.default
    validator = extras.get('validator', None)
    is_required = extras.get('required', False)
    nargs = extras.get('nargs', None)
    arg_default = default if default is MISSING else repr(default)
    args: AnyMap = {
        'help': argument_description(field.description or '', arg_default),
    }
    if default is not MISSING:
        args['default'] = default

    if validator:
        args['type'] = validator

    if nargs:
        args['nargs'] = nargs
    elif not is_required:
        args['nargs'] = '?'

    return FuncArgs(args=[name], kwargs=args)

should_handle(extras)

Handle a positional field.

Parameters:

Name Type Description Default
extras AnyMap

A dictionary with information for a cli argument.

required

Returns:

Type Description
bool

True if it should handle the field as positional

Source code in m/cli/engine/parsers/positional.py
def should_handle(extras: AnyMap) -> bool:
    """Handle a positional field.

    Args:
        extras: A dictionary with information for a cli argument.

    Returns:
        True if it should handle the field as positional
    """
    return extras.get('positional', False) is True