kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.41k stars 134 forks source link

pyinstaller support #355

Closed RagingPuppies closed 3 years ago

RagingPuppies commented 3 years ago

Hi, once i compile my project to onefile executable with pyinstaller seems like only the arguments get autocompleted, no completers work, either from argparse "choices" nor from:

import argcomplete, argparse

def bla_completion_list(prefix, parsed_args, **kwargs):
    bla = ['ALL','test']
    return bla

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description="")
    parser.add_argument("--exclude-bla", dest="excluded", nargs='+',
                      help="space separated list of bla to ignore, will override included, ALL will include all bla",
                      default='').completer = bla_completion_list

    parser.add_argument("--dest-dc", dest="dest_dc", choices=('DC1','DC2','DC3'), help="'DC1','DC2','DC3'", default='')

    argcomplete.autocomplete(parser)

    args = parser.parse_args()

did anyone found a solution for this use-case? this is something supported? the # PYTHON_ARGCOMPLETE_OK was bypassed by providing the bash a filename instead of reading the string in the py file.

Thanks

evanunderscore commented 3 years ago

Thanks for the report. I believe this should work, since argcomplete should theoretically just invoke whatever name you give it; it shouldn't matter how the script is implemented. Can you try setting _ARC_DEBUG=1 and pasting the output of a completion attempt?

RagingPuppies commented 3 years ago

@evanunderscore sure Evan, here's the output for choices:

(root@bla-server:dist)# ./test --dest-dc
word --dest-dc split, lexer state: ' '
In trailing whitespace

LINE: './test --dest-dc '
POINT: 17
PREQUOTE: ''
PREFIX: ''
SUFFIX: ''
WORDS: []
Active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
Visited positionals: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
invoking parser with []
parsed args: (Namespace(dest_dc='', excluded=''), [])
all active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
active_parser: MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
optional options: ['-h', '--help', '--exclude-bla', '--dest-dc']
next_positional: None
Active actions (L=0): []
active options: ['-h', '--help', '--exclude-bla', '--dest-dc']
display completions: {('-h', '--help'): 'show this help message and exit', ('--exclude-bla',): 'space separated list of bla to ignore, will override included, ALL will include all bla', ('--dest-dc',): "'DC1','DC2','DC3'"}

Returning completions: ['-h', '--help', '--exclude-bla', '--dest-dc']
-

and heres output for the bla function

(root@bla-server:dist)# ./test --ex
word --ex split, lexer state: 'None'

LINE: './test --ex'
POINT: 11
PREQUOTE: ''
PREFIX: '--ex'
SUFFIX: ''
WORDS: []
Active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
Visited positionals: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
invoking parser with []
parsed args: (Namespace(dest_dc='', excluded=''), [])
all active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
active_parser: MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
optional options: ['--exclude-bla']
next_positional: None
Active actions (L=0): []
active options: ['--exclude-bla']
display completions: {(): "'DC1','DC2','DC3'", ('--exclude-bla',): 'space separated list of bla to ignore, will override included, ALL will include all bla'}

Returning completions: ['--exclude-bla']
clude-bla
word --exclude-bla split, lexer state: ' '
In trailing whitespace

LINE: './test --exclude-bla '
POINT: 21
PREQUOTE: ''
PREFIX: ''
SUFFIX: ''
WORDS: []
Active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
Visited positionals: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
invoking parser with []
parsed args: (Namespace(dest_dc='', excluded=''), [])
all active parsers: [MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)]
active_parser: MonkeyPatchedIntrospectiveArgumentParser(prog='test', usage=None, description='', formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
optional options: ['-h', '--help', '--exclude-bla', '--dest-dc']
next_positional: None
Active actions (L=0): []
active options: ['-h', '--help', '--exclude-bla', '--dest-dc']
display completions: {('-h', '--help'): 'show this help message and exit', ('--exclude-bla',): 'space separated list of bla to ignore, will override included, ALL will include all bla', ('--dest-dc',): "'DC1','DC2','DC3'"}

Returning completions: ['-h', '--help', '--exclude-bla', '--dest-dc']
-

as you can see, it auto complete the arguments, but not the choices... let me know if there's anything i can do here.

evanunderscore commented 3 years ago

For whatever reason, it's not parsing out the tokens (words) correctly.

LINE: './test --dest-dc '
POINT: 17
PREQUOTE: ''
PREFIX: ''
SUFFIX: ''
WORDS: []

Here's what it should look like:

LINE: './test --dest-dc '
POINT: 17
PREQUOTE: ''
PREFIX: ''
SUFFIX: ''
WORDS: ['./test', '--dest-dc']

This actually appears to be working fine for me with pyinstaller (at least on Windows):

LINE: './test.exe --dest-dc '
POINT: 21
PREQUOTE: ''
PREFIX: ''
SUFFIX: ''
WORDS: ['./test.exe', '--dest-dc']

The relevant code is here. I can't see how that would be going wrong, unless _ARGCOMPLETE is somehow set to the wrong number. Your values of COMP_LINE and COMP_POINT appear correct, so something has to be going wrong in the linked section of code. You're welcome to do some debugging yourself, or if you can provide a complete set of instructions for how to replicate your build, I can try again to replicate your issue.

RagingPuppies commented 3 years ago

Hi Thanks @evanunderscore my issue was multiple duplicates bash scripts in the bash_completion folder, once cleaned it works :D thanks a lot!

evanunderscore commented 3 years ago

No problem, glad you got it sorted!