用于打印问题的python

时间:2018-03-15 06:31:16

标签: python python-3.x

以下是我的Perl脚本的一部分。如您所见,此脚本将打印出有关脚本的使用信息。第一个_EOF_是打印信息的开始标记,第二个_EOF是打印信息的结束标记。并且所有信息将保持与我输入的格式相同。

use strict;
use Getopt::Long;

GetOptions(
    'h|help'                => \$help,
) or usage();


usage() if $help;

sub usage {
        #$"=',';
        print STDERR <<_EOF_;

xmlcollect [-h] [-g]  [-d <spooldir>] [-m <pmpath>]  [-f <regex>] [-x] [-norbs] [-nornc] [-noenb] [-maxrop <num>] [-job <name>] [-subnet <subnetwork>]

Fetch XML files from a directory and store the file collection in a zip
file ready for collection.

Each time the tool is run, it will check all available files and only
include unfetched files in the output zip.  Only once instance of the 
collection
is permitted at any time.

-h       : Show help
-g       : Debug mode
-d       : Spool directory (default: $spooldir)
-m       : PM dirs to search on nodes, comma separated list (default: @pmpaths)
-f       : If set, any PM files on the node will only be fetched if they 
match this regular expression.(default: $filematch)
-x       : Don't fetch any files. Mark all files on the node as already fetched
           This can be used the first time you run xmlcollect, so that only
           files fetched from this point in time on will be fetched.
-norbs   : Don't attempt to fetch RBS files
-nornc   : Don't attempt to fetch RNC files
-noenb   : Don't attempt to fetch ENB files
-maxrop  : Maximum quantities of ropfile per node
           Ordered from most recent to the oldest
-job     : String job name
-subnet  : eNode-B Subnetwork name regex

_EOF_
        exit;
}

enter image description here

我想用python实现同样的效果。期望类型很多print,还有其他方式吗?非常感谢你。

1 个答案:

答案 0 :(得分:0)

只需使用所需字符串调用print并将file参数设置为sys.stderr

import sys

usage = """xmlcollect [-h] [-g]  [-d <spooldir>] [-m <pmpath>]  [-f <regex>] [-x] [-norbs] [-nornc] [-noenb] [-maxrop <num>] [-job <name>] [-subnet <subnetwork>]

Fetch XML files from a directory and store the file collection in a zip
file ready for collection.
etc....."""

print(usage, file=sys.stderr)