Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Selecting from a predefined list #167

Open
BjornFJohansson opened this issue Mar 7, 2024 · 3 comments
Open

[Question] Selecting from a predefined list #167

BjornFJohansson opened this issue Mar 7, 2024 · 3 comments
Labels
invalid Doesn't seem right

Comments

@BjornFJohansson
Copy link

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()
@BjornFJohansson BjornFJohansson changed the title Selecting from a predefined list [Question] Selecting from a predefined list Mar 7, 2024
@casperdcl
Copy link
Collaborator

not sure what the titleselector is for. You can just remove the .complete = titleselector bit and probably also add a prog to ArgumentParser.

@casperdcl casperdcl added the invalid Doesn't seem right label Mar 7, 2024
@BjornFJohansson
Copy link
Author

Hi, and thanks for the feedback. The tptitles is a list with three strings. I would like to autocomplete one of the options from this list.

@casperdcl
Copy link
Collaborator

casperdcl commented Mar 8, 2024

argparse choices are fully supported.

#!/usr/bin/env python3
import argparse, shtab
tptitles = """
TP01_Calculating_checksums_using_seguid_calculator
TP02_Visualizing_linear_and_circular_DNA_with_ApE
TP03_In-silico_sub_cloning_using_ApE
""".strip().split()
parser = argparse.ArgumentParser(prog='tpcorrect', description='Correct TPs')
shtab.add_argument_to(parser, ["-s", "--print-completion"])  # magic!
parser.add_argument('title', choices=tptitles, help='tp to correct.')
if __name__ == '__main__':
    args = parser.parse_args()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants