4646import pydoc
4747import types
4848from cStringIO import StringIO
49+ from optparse import OptionParser
4950from urlparse import urljoin
5051from xmlrpclib import ServerProxy , Error as XMLRPCError
5152from ConfigParser import ConfigParser , NoSectionError , NoOptionError
6566 Group , OneOrMore , ZeroOrMore , Literal , Optional , Word , \
6667 alphas , alphanums , printables , ParseException
6768
69+ from bpython import __version__
70+
6871def log (x ):
6972 f = open ('/home/bob/tmp/bpython.out' , 'a' )
7073 f .write ('%s\n ' % (x ,))
@@ -1794,7 +1797,7 @@ def migrate_rc(path):
17941797
17951798
17961799
1797- def loadini ():
1800+ def loadini (configfile ):
17981801 """Loads .ini configuration file and stores its values in OPTS"""
17991802 class CP (ConfigParser ):
18001803 def safeget (self , section , option , default ):
@@ -1812,10 +1815,7 @@ def safeget(self, section, option, default):
18121815 except ValueError :
18131816 return v
18141817
1815- if len (sys .argv ) > 2 :
1816- configfile = sys .argv [2 ]
1817- else :
1818- configfile = os .path .expanduser ('~/.bpython.ini' )
1818+ configfile = os .path .expanduser (configfile )
18191819
18201820 config = CP ()
18211821 config .read (configfile )
@@ -1887,7 +1887,24 @@ def main_curses(scr):
18871887 return repl .getstdout ()
18881888
18891889
1890- def main ():
1890+ def main (args = None ):
1891+ if args is None :
1892+ args = sys .argv
1893+
1894+ parser = OptionParser ()
1895+ parser .add_option ('--config' , '-c' , default = '~/.bpython.ini' ,
1896+ help = 'use CONFIG instead of default config file' )
1897+ parser .add_option ('--version' , '-V' , action = 'store_true' ,
1898+ help = 'print version and exit' )
1899+
1900+ options , args = parser .parse_args (args )
1901+
1902+ if options .version :
1903+ print 'bpython version' , __version__ ,
1904+ print 'on top of Python' , sys .version .split ()[0 ]
1905+ print '(C) 2008-2009 Bob Farrell et al. See AUTHORS for detail.'
1906+ return
1907+
18911908 if not os .isatty (sys .stdin .fileno ()):
18921909 interpreter = code .InteractiveInterpreter ()
18931910 interpreter .runsource (sys .stdin .read ())
@@ -1898,7 +1915,7 @@ def main():
18981915 path = os .path .expanduser ('~/.bpythonrc' ) # migrating old configuration file
18991916 if os .path .isfile (path ):
19001917 migrate_rc (path )
1901- loadini ()
1918+ loadini (options . config )
19021919
19031920 try :
19041921 o = curses .wrapper (main_curses )
0 commit comments