def _parse_command_flags()

in ec2instanceconnectcli/input_parser.py [0:0]


def _parse_command_flags(raw_command, instance_bundles, is_ssh=False):
    """
    Parses the command from the user and strips out two pieces:
    1) The flags for the underlying command
    2) The actual underlying command or file list for ssh/sftp/etc

    :param raw_command: The raw command string, ie, anything not recognized by argparse
    :type raw_command: basestring
    :param instance_bundles: dicts containing information about desired EC2 instances
    :type instance_bundles: list
    :param is_ssh: Specifies if we are running an ssh command.  There is an extra flag we consider if so.
    :type is_ssh: bool
    :return: tuple of flags and final comamnd or file list
    :rtype: tuple
    """
    flags = ''
    is_user = False
    is_flagged = False
    command_index = 0
    used = 0
    """
    Flags for the underlying command.  These will always be in the format -[flag indicator] [flag value]
    """
    while command_index < len(raw_command) - 1:
        if raw_command[command_index][0] != '-' and not is_flagged:
            # We found something that's not a flag or a flag value.  Exit flag loop.
            break

        used += 1

        # This is either a flag or a flag value
        flags = '{0} {1}'.format(flags, raw_command[command_index])

        if raw_command[command_index][0] == '-':
            # Flag
            is_flagged = True
            if raw_command[command_index][1] == 'l' and is_ssh:
                # We want to extract the user flag for ssh mode
                is_user = True

        else:
            # Flag value
            is_flagged = False
            if is_user:
                # We want to extract the user flag for ssh mode
                instance_bundles[0]['username'] = raw_command[command_index]
                is_user = False

        command_index += 1

    flags = flags.strip()

    """
    Target host and command or file list
    """

    if used == len(raw_command) and len(raw_command) != 1:
        # EVERYTHING was a flag or flag value
        raise AssertionError('Missing target')

    # Target
    instance_bundles[0]['target'] = raw_command[command_index]
    command_index += 1

    # Command/file list
    command_end = len(raw_command)
    command = ' '.join(raw_command[command_index:command_end])

    return flags, command, instance_bundles