#!/usr/bin/env python3
"""
Command line utility for converting integers between bases.

Accepts an integer in [b]inary, [o]ctal, [d]ecimal, or he[x] base. 
Prints it in all four bases. A single letter prefix declares the base 
of the input. (Defaults to decimal if no prefix is present.)

Accepts multiple arguments.

Example:

    $ int x10
    
    bin: 10000
    oct: 20
    dec: 16
    hex: 10

License: This work has been placed in the public domain.

"""

__version__ = '0.1.0'


import os, sys


help_text = """usage: %s INT [INT ...]

Print an integer in its [b]inary, [o]ctal, [d]ecimal, and he[x] bases.

Use a single letter prefix to declare the base of the input, e.g. b1010. 
The base defaults to decimal if no prefix is supplied. Multiple arguments 
are accepted.""" % os.path.basename(__file__)


template = """
bin: {0:b}
oct: {0:o}
dec: {0:d}
hex: {0:X}\
"""


bases = {
    'b': (2, 'a binary'),
    'o': (8, 'an octal'),
    'd': (10, 'a decimal'),
    'x': (16, 'a hexadecimal'),
}


def print_ints(args):
    for argstr in args:
        prefix = argstr[0].lower()
        if prefix in bases:
            intstr = argstr[1:]
            base, adj = bases[prefix]
        else:
            intstr = argstr
            base, adj = bases['d']
        try:
            print(template.format(int(intstr, base)))
        except ValueError:
            print("\nerror: [%s] cannot be parsed as %s integer" % (intstr, adj))


if __name__ == '__main__':
    if len(sys.argv) == 1 or sys.argv[1] in ('-h', '--help'):
        print(help_text)
    else:
        print_ints(sys.argv[1:])


@@ -348,53 +348,56 @@
     
-bin: 10000%0A    oct: 20%0A    dec: 16%0A    hex: 1
+hex: 10%0A    dec: 16%0A    oct: 20%0A    bin: 0001000
 0%0A%0AL
@@ -475,17 +475,17 @@
 __ = '0.
-1
+2
 .0'%0A%0A%0Aim
@@ -544,16 +544,17 @@
 %5D%0A%0APrint
+s
  an inte
@@ -828,51 +828,47 @@
 %22%22%22%0A
-bin
+hex
 : %7B0:
-b%7D%0Aoct
+X%7D%0Adec
 : %7B0:
-o%7D%0Adec
+,d%7D%0Aoct
 : %7B0:
-d%7D%0Ahex: %7B0:X%7D
+o%7D%0Abin: 
 %5C%0A%22%22
@@ -993,17 +993,291 @@
 imal'),%0A
-%7D
+    'h': (16, 'a hexadecimal'),%0A%7D%0A%0A%0Adef binary_string(i):%0A    s = ''%0A    while i:%0A        for digit in range(8):%0A            if i & 1 == 1:%0A                s = '1' + s%0A            else:%0A                s = '0' + s%0A            i %3E%3E= 1%0A        s = ' ' + s%0A    return s.lstrip()
 %0A%0A%0Adef p
@@ -1386,33 +1386,34 @@
 :%0A            in
-t
+p_
 str = argstr%5B1:%5D
@@ -1479,17 +1479,18 @@
       in
-t
+p_
 str = ar
@@ -1558,37 +1558,26 @@
     
-print(template.format(
+inp_int = 
 int(in
-t
+p_
 str,
@@ -1582,18 +1582,16 @@
 r, base)
-))
 %0A       
@@ -1626,15 +1626,35 @@
     
-print(%22
+sys.exit(%0A                '
 %5Cner
@@ -1662,12 +1662,12 @@
 or: 
-%5B%25s%5D
+%22%25s%22
  can
@@ -1697,16 +1697,17 @@
 eger
-%22
+'
  %25 (in
-t
+p_
 str,
@@ -1715,37 +1715,100 @@
 adj)
-)%0A%0A%0Aif __name__ == '__
+%0A            )%0A        print(template.format(inp_int) + binary_string(inp_int))%0A%0A%0Adef 
 main
-__'
+()
 :%0A  
@@ -1834,43 +1834,84 @@
 == 1
- or sys.argv%5B1%5D in ('-h', '--help')
+:%0A        print(help_text)%0A    elif '-h' in sys.argv or '--help' in sys.argv
 :%0A  
@@ -1977,8 +1977,47 @@
 v%5B1:%5D)%0A%0A
+%0Aif __name__ == '__main__':%0A    main()%0A

Print binary representation in octet form