Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4.  
  5. def _help():
  6. res = """Usage:
  7. more [options] <file>...
  8.  
  9. A file perusal filter for CRT viewing.
  10.  
  11. Options:
  12. -s squeeze multiple blank lines into one
  13. +<number> display file beginning from line number
  14. -V display version information and exit
  15. """
  16. return res
  17.  
  18. def more(options, fname):
  19. exit_options = {
  20. '-V': lambda: 'more from util-linux 2.27'
  21. }
  22. for option in options:
  23. if option in exit_options:
  24. return exit_options[option]()
  25. output_options = {
  26. '-s': lambda s: '\n'.join([line for line in s.split('\n') if line])
  27. }
  28. with open(fname, 'r') as f:
  29. res = f.read()
  30. for option in options:
  31. if option in output_options:
  32. res = output_options[option](res)
  33. lines = res.split('\n')
  34. for i in range(0, len(lines), 30):
  35. print('\n'.join(lines[i:i + 30]))
  36. input('')
  37. return None
  38.  
  39. if __name__ == '__main__':
  40. res = None
  41. if len(sys.argv) < 2:
  42. res = _help()
  43. else:
  44. options = []
  45. for token in sys.argv[1:]:
  46. if token.startswith('-'):
  47. options = options + [token]
  48. fname = sys.argv[-1]
  49. res = more(options, fname)
  50. if res:
  51. print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement