Demo entry 5913296

Hi there

   

Submitted by anonymous on Sep 01, 2016 at 15:59
Language: Python. Code size: 1.1 kB.

from __future__ import print_function
import sys
from collections import deque

def align_str(s, side='left', fillchar='', line_length=80):
    if not s:
        return None
    if side == 'left':
        formatstr =  '{:%s<%d}' % (fillchar, line_length)
    elif side == 'right':
        formatstr =  '{:%s>%d}' % (fillchar, line_length)
    elif side == 'center':
        formatstr =  '{:%s^%d}' % (fillchar, line_length)
    else:
        return s
    return formatstr.format(s)

def usage(cmdname):
    print('Usage: {} <string> [<side> [<length>]]'.format(cmdname))
    print('Example: {} "hello world" left 40'.format(cmdname))

if __name__ == '__main__':
    if len(sys.argv) == 1:
        print('Not enough arguments.')
        usage(sys.argv[0])
        sys.exit(-1)
    queue = deque(sys.argv[1:])
    s = ''
    posn = 'left'
    size = 40
    fill = ''
    if queue:
        s = queue.popleft()
    if queue:
        posn = queue.popleft()
    if queue:
        size = queue.popleft()
    if queue:
        fill = queue.popleft()

    print('<', align_str(s, side=posn, line_length=int(size), fillchar=fill), '>')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).