I have the following arguments which are to be parsed using argparse
- input_dir (string: Mandatory)
- output_dir (string: Mandatory)
- file_path (string: Mandatory)
- supported_file_extensions (comma separated string - Optional)
- ignore_tests (boolean - Optional)
If either comma separated string and a string are provided for supported_file_extensions and ignore_tests respectively, then I want the value to be set to the argument variable. My set up is as follows
import sys
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'input_dir', type=str,
        help='Fully qualified directory for the input directory')
    parser.add_argument(
        'file_path', type=str,
        help='Fully  qualified path name for file')
    parser.add_argument(
       'output', type=str, help='Fully qualified output directory path')
    parser.add_argument(
        '-supported_file_extensions',nargs='?', type=str, default=None,
        help='optional comma separated file extensions'
    ) # Should default to False
    parser.add_argument(
        '-ignore_tests', nargs='?', type=bool, default=True
    ) # Should default to True if the argument is passed
    (arguments, _) = parser.parse_known_args(sys.argv[1:])
    print(arguments)
When I pass the following command
 python cloc.py -input_dir /myproject -file_path /myproject/.github/CODEOWNERS -output output.json --supported_file_extensions java,kt --ignore_tests False
I want the value of the arguments.--supported_file_extensions to be equal to java,kt and ignore_tests to equal to False but I get the following values
Namespace(file_path='/myproject/.github/CODEOWNERS', ignore_tests=True, input_dir='/myproject', output='output.json', supported_file_extensions=None)
If I don't pass --ignored_tests in my command line argument, then I want the file to default to False. Similarly, when I pass --ignored_tests, then I want the argument to be set to True.
If I pass --supported_file_extensions java,kt, then I want the argument to be set to java,kt as a string, and not a list.
I have tried going through but I have not had any success.
- Python argparse: default value or specified value
- python argparse default value for optional argument
- Creating command lines with python (argparse)
UPDATE I updated the last two arguments as follows:
parser.add_argument(
        '-supported_file_extensions', type=str,
        help='optional comma separated file extensions'
    )
    parser.add_argument(
        '-ignore_tests', action='store_true',
        help='optional flag to ignore '
    )
    (arguments, _) = parser.parse_known_args(sys.argv[1:])
    print(arguments)
When I run the command as
  python cloc.py -input_dir /myproject -file_path /myproject/.github/CODEOWNERS -output output.json --supported_file_extensions java,kt
The output is as follows
 Namespace(file_path='/myproject/.github/CODEOWNERS', ignore_tests=False, input_dir='/myproject', output='output.json', supported_file_extensions=None)
While the value of ignore_tests is correct, (defaulted to false), the value of supported_file_extensions is None, which is not correct since I had passed java,kt as command line arguments.
When I pass
 python cloc.py -input_dir /myproject -file_path /myproject/.github/CODEOWNERS -output output.json --supported_file_extensions java,kt --ignore_tests 
I get the following output
Namespace(file_path='/myproject/.github/CODEOWNERS', ignore_tests=False, input_dir='/myproject', output='output.json', supported_file_extensions=None)
 
     
    