forked frombpython/bpython
- Notifications
You must be signed in to change notification settings - Fork0
Expand file tree
/
Copy pathargs.py
More file actions
115 lines (94 loc) · 4.13 KB
/
args.py
File metadata and controls
115 lines (94 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Module to handle command line argument parsing, for all front-ends.
"""
from __future__importwith_statement
importos
importsys
importcode
fromoptparseimportOptionParser,OptionGroup
frombpythonimport__version__
frombpython.configimportloadini,Struct,migrate_rc
classOptionParserFailed(ValueError):
"""Raised by the RaisingOptionParser for a bogus commandline."""
classRaisingOptionParser(OptionParser):
deferror(self,msg):
raiseOptionParserFailed()
defparse(args,extras=None):
"""Receive an argument list - if None, use sys.argv - parse all args and
take appropriate action. Also receive optional extra options: this should
be a tuple of (title, description, options)
title: The title for the option group
description: A full description of the option group
options: A list of optparse.Option objects to be added to the
group
e.g.:
parse(['-i', '-m', 'foo.py'],
('Front end-specific options',
'A full description of what these options are for',
[optparse.Option('-f', action='store_true', dest='f', help='Explode'),
optparse.Option('-l', action='store_true', dest='l', help='Love')]))
Return a tuple of (config, options, exec_args) wherein "config" is the
config object either parsed from a default/specified config file or default
config options, "options" is the parsed options from
OptionParser.parse_args, and "exec_args" are the args (if any) to be parsed
to the executed file (if any).
"""
ifargsisNone:
args=sys.argv[1:]
parser=RaisingOptionParser(
usage='Usage: %prog [options] [file [args]]\n'
'NOTE: If bpython sees an argument it does '
'not know, execution falls back to the '
'regular Python interpreter.')
# This is not sufficient if bpython gains its own -m support
# (instead of falling back to Python itself for that).
# That's probably fixable though, for example by having that
# option swallow all remaining arguments in a callback.
parser.disable_interspersed_args()
parser.add_option('--config','-c',default='~/.bpython/config',
help='use CONFIG instead of default config file')
parser.add_option('--interactive','-i',action='store_true',
help='Drop to bpython shell after running file '
'instead of exiting')
parser.add_option('--quiet','-q',action='store_true',
help="Don't flush the output to stdout.")
parser.add_option('--version','-V',action='store_true',
help='print version and exit')
ifextrasisnotNone:
extras_group=OptionGroup(parser,extras[0],extras[1])
foroptioninextras[2]:
extras_group.add_option(option)
parser.add_option_group(extras_group)
try:
options,args=parser.parse_args(args)
exceptOptionParserFailed:
# Just let Python handle this
os.execv(sys.executable, [sys.executable]+args)
ifoptions.version:
print'bpython version',__version__,
print'on top of Python',sys.version.split()[0]
print ('(C) 2008-2010 Bob Farrell, Andreas Stuehrk et al. '
'See AUTHORS for detail.')
raiseSystemExit
ifnot (sys.stdin.isatty()andsys.stdout.isatty()):
interpreter=code.InteractiveInterpreter()
interpreter.runsource(sys.stdin.read())
raiseSystemExit
path=os.path.expanduser('~/.bpythonrc')
# migrating old configuration file
ifos.path.isfile(path):
migrate_rc(path)
config=Struct()
loadini(config,options.config)
returnconfig,options,args
defexec_code(interpreter,args):
"""
Helper to execute code in a given interpreter. args should be a [faked]
sys.argv
"""
withopen(args[0],'r')assourcefile:
code_obj=compile(sourcefile.read(),args[0],'exec')
old_argv,sys.argv=sys.argv,args
sys.path.insert(0,os.path.abspath(os.path.dirname(args[0])))
interpreter.runcode(code_obj)
sys.argv=old_argv