Abdul Ahad
Abdul Ahad

Reputation: 31

How to convert command line code to simple code?

So, I am trying to pass a code to main(), the original code is command line based (It takes arguments from command line interface) but I want to pass it to main() directly in program. Here is the original code.

if __name__ == '__main__':
    log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_fmt)

    parser = argparse.ArgumentParser()
    parser.add_argument('json_in')
    parser.add_argument('graph_out')
    parser.add_argument('text_out')
    parser.add_argument('spm_model')
    parser.add_argument('--sequence', action='store_true')
    parser.add_argument('--simple-preprocessing',
                        action='store_false', dest='extended_preprocessing')
    parser.add_argument('--bpe-dropout', type=float, default=None)
    parser.add_argument('--sample-factor', type=int, default=1)

    args = parser.parse_args()
    main(args) 

Now I understand how to convert each line except 11th one, line including the --sequence.

I don't understand what to pass for dest in program.

I am actually working on colab and it throws as error SystemExit: 2 with argparse, thats why I dont want to use it anyway.

Upvotes: 1

Views: 255

Answers (1)

Cor
Cor

Reputation: 134

The flags with a - prefix, are accessed by removing the - prefix and making the non-prefix - underscores _, if there is no dest defined.

parser.add_argument('--sequence', action='store_true')
parser.add_argument('--simple-preprocessing',
                    action='store_false', dest='extended_preprocessing')
parser.add_argument('--bpe-dropout', type=float, default=None)
parser.add_argument('--sample-factor', type=int, default=1)

args = parser.parse_args()

args.sequence 
args.extended_preprocessing     # use dest
args.bpe_dropout
args.sample_factory

Upvotes: 1

Related Questions