# HG changeset patch # User Jeff Hammel # Date 1426094120 25200 # Node ID 8addd6e12b29a105b8a483ab05194354653b77cb # Parent 21b6a9569f21426f6b9030eec6b755d63339732d corename diff -r 21b6a9569f21 -r 8addd6e12b29 setup.py --- a/setup.py Wed Sep 03 18:13:15 2014 -0700 +++ b/setup.py Wed Mar 11 10:15:20 2015 -0700 @@ -12,12 +12,13 @@ try: from setuptools import setup kw['entry_points'] = """ - [console_scripts] - indent = textshaper.indent:main - onelineit = textshaper.onelineit:main - quote = textshaper.quote:main - textshaper = textshaper.main:main - url2txt = textshaper.url2txt:main + [console_scripts] + corenames = textshaper.corename:main + indent = textshaper.indent:main + onelineit = textshaper.onelineit:main + quote = textshaper.quote:main + textshaper = textshaper.main:main + url2txt = textshaper.url2txt:main """ kw['install_requires'] = dependencies except ImportError: diff -r 21b6a9569f21 -r 8addd6e12b29 textshaper/corename.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/textshaper/corename.py Wed Mar 11 10:15:20 2015 -0700 @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +echo core name of files +""" + +# imports +import argparse +import os +import sys + +# module globals +__all__ = ['main', 'Parser'] + +def ensure_dir(directory): + """ensure a directory exists""" + if os.path.exists(directory): + if not os.path.isdir(directory): + raise OSError("Not a directory: '{}'".format(directory)) + return directory + os.makedirs(directory) + return directory + + +class Parser(argparse.ArgumentParser): + """CLI option parser""" + def __init__(self, **kwargs): + kwargs.setdefault('formatter_class', argparse.RawTextHelpFormatter) + kwargs.setdefault('description', __doc__) + argparse.ArgumentParser.__init__(self, **kwargs) + self.add_argument('files', nargs='*', + help="files, else read from stdin") + self.options = None + + def parse_args(self, *args, **kw): + options = argparse.ArgumentParser.parse_args(self, *args, **kw) + self.validate(options) + self.options = options + return options + + def validate(self, options): + """validate options""" + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line options + parser = Parser() + options = parser.parse_args(args) + + if not options.files: + options.files = sys.stdin.read().strip().split() + + corenames = [os.path.splitext(os.path.basename(f))[0] + for f in options.files] + print (" ".join(corenames)) + +if __name__ == '__main__': + main() + +