from jcvi.apps.base import OptionParser def main(): parser = OptionParser(description="A simple script") parser.add_option("--verbose", action="store_true", dest="verbose", help="Verbose output") options, args = parser.parse_args() if options.verbose: print("Verbose output enabled") if __name__=="__main__": main()
from jcvi.apps.base import OptionParser def main(): parser = OptionParser(usage="usage: %prog [options] input_file") parser.add_option("--output", dest="output_file", help="Output file name") options, args = parser.parse_args() if len(args) != 1: parser.error("Incorrect number of input files") input_file = args[0] output_file = options.output_file or input_file + ".out" print("Input file:", input_file) print("Output file:", output_file) if __name__=="__main__": main()In this example, we define a usage message for the script and a required input file argument. We also add an optional output file argument (--output) and provide a default value if it's not specified. The main function then processes the input and output filenames and outputs them. Overall, OptionParser is a useful tool for defining command-line options in Python scripts. JCvi is the package library that contains it.