2

There is a file argComp.py in Ubuntu 14.04 LTS. I install argcomplete 1.0.0 module successfully.

chmod +x argComp.py

and then

data = [ name1 : { address1 : [Korea, Seoul ] }, name2 : { address2 : [ USA, LA ] } ]

./argComp.py --name [tab]

./argComp.py --name
name1 name2

./argComp.py --name name1 --address [tab]
Korea Seoul

I want read the my argument by realtime before the user presses ENTER. For this, the argcomplete module should read text name in --name name1, and then check data by name1.

I could I get data and make the --address list ?

I want to implement this concept to file browser realtime..

Please help, and thank you in advance.

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

0

It's a bit difficult to understand what you're asking but I think this might be what you want:

Argcomplete only work when you use argparse to manage your command line arguments.

For your example argComp.py should be something like this

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
import argcomplete, argparse

parser = argparse.ArgumentParser(description='Test passing arguments')
parser.add_argument('--name1', metavar='NAME1', nargs=1, choices=['Korea', 'Seoul'], help="First address line")
parser.add_argument('--name2', metavar='NAME2', nargs=1, choices=['USA', 'LA'], help="Second address line")

argcomplete.autocomplete(parser)
args = parser.parse_args()

# Your code here
# Print the arguments
print(args.name1[0],"-",args.name2[0])

You can run argComp.py by typing

python argComp.py --name1 Korea --name2 LA

To print usage details

python argComp.py -h

If you want it to be fully dynamic you can store your arguments in json format then pass it as environment variable

lets say that we want to parse the following data (json format)

'{"name1": {"address": ["korea", "Seoul"], "gender": ["Male","Female"]}, "name2": {"address": ["USA", "LA"]} }'

then we will store it in an environment variable, lets name it PY_ARGS so in the terminal:

$ PY_ARGS='{"name1": {"address": ["korea", "Seoul"], "gender": ["Male","Female"]}, "name2": {"address": ["USA", "LA"]} }'
$ export PY_ARGS

Of course you can set this environment variable from any place you want i.e. another program.

Now our program argComp.py will be

import argcomplete, argparse
import os
import json

args = os.environ['PY_ARGS'] # get the environment variable

# Parse env variable to dictionary
args_dict = json.loads(args)

# create the top-level parser
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subparser')

# Get keys as list
top_level_keys = list(args_dict.keys()) # [name1, name2]

for i in range(0,len(top_level_keys)):
    key = top_level_keys[i] # when i=0 name1
    sub_dict = args_dict[key] # when i=0 {"address": ["korea", "Seoul"], "gender": ["Male","Female"]}
    sub_parser = subparsers.add_parser(key)
    sub_level_keys = list(sub_dict.keys()) # when i=0 [address, gender]

    for k in range(0, len(sub_level_keys)):
        sub_key = sub_level_keys[k] # when k=0 address
        choices = sub_dict[sub_key] # when k=0 [korea, Seoul]
        sub_parser.add_argument('--'+sub_key, nargs=1, choices=choices)


argcomplete.autocomplete(parser)
args = parser.parse_args()

# Your code here
print(args)

Now we can run argComp.py

$ ./argComp.py name1 --gender Male
Namespace(address=None, gender=['Male'], subparser='name1')

Or with address

$ ./argComp.py name1 --gender Male --address korea
Namespace(address=['korea'], gender=['Male'], subparser='name1')

This will print error

$ ./argComp.py name2 --gender Male --address korea
usage: argComp.py name2 [-h] [--address {USA,LA}]
argcomplete2.py name2: error: argument --address: invalid choice: 'korea' (choose from 'USA', 'LA')

help is nicely printed

$ ./argComp.py -h
usage: argComp.py [-h] {name1,name2} ...

positional arguments:
  {name1,name2}

optional arguments:
  -h, --help     show this help message and exit

and

$ ./argComp.py name1 -h
usage: argComp.py name1 [-h] [--gender {Male,Female}]
                             [--address {korea,Seoul}]

optional arguments:
  -h, --help            show this help message and exit
  --gender {Male,Female}
  --address {korea,Seoul}

The trick here is to create dictionary from your json string and add subparser(s).

You can add more sub levels to match your needed arguments structure.

alper
  • 2,919
  • 9
  • 53
  • 102
MMayla
  • 199
  • 1
  • 14
  • Thank you.. But I mean if there is no data set. How could I get the argument in buffer and use this argument for checking data realtime I press [TAB] before I [ENTER]... Your example is absolute path. My data set is just example.. – Hyunjun Cheong Mar 28 '16 at 07:39
  • @HyunjunCheong check my edit. I think this is what you want – MMayla Mar 28 '16 at 21:16