Skip to content

[Question] Selecting from a predefined list #167

Open
@BjornFJohansson

Description

Hi all, I am trying to use shtab for the first time. I would like to autocomplete a positional argument from a
list. I made a choice function titleselector that selects from a list based on a prefix. This does not work for me, but
I suspect that there is an error in how I try to use shtab. I suspect that this use case is a common one? I could not find any code examples for this.

Help appreciated!

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import shtab  # for completion magic

tptitles = """\
TP01_Calculating_checksums_using_seguid_calculator
TP02_Visualizing_linear_and_circular_DNA_with_ApE
TP03_In-silico_sub_cloning_using_ApE
""".splitlines()

def titleselector(prefix):
    return [title for title in tptitles if title.startswith(prefix)]


parser = argparse.ArgumentParser(description='Correct TPs')
shtab.add_argument_to(parser, ["-s", "--print-completion"])  # magic!

parser.add_argument('title',
                    choices=tptitles,
                    help='tp to correct.').complete = titleselector

parser.add_argument('--student',
                    dest='studentinfo',
                    action='store',
                    help='a string thad identifies the student.')

args = parser.parse_args()

Metadata

Assignees

No one assigned

    Labels

    invalidDoesn't seem right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions