import shcol
import sys
import unittest

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

DUMMY_ITEM = 'spam'

class ArgumentParserTest(unittest.TestCase):
    def setUp(self):
        self.parser = shcol.cli.ArgumentParser('shcol', shcol.__version__)

    def _get_stderr_output(self, args):
        old_stderr = sys.stderr
        sys.stderr = StringIO()
        result = ''
        try:
            self.parser.parse_args(args)
        except SystemExit:
            result = sys.stderr.getvalue().rstrip('\n')
        finally:
            sys.stderr = old_stderr
        return result

    def test_version(self):
        expected = '{} {}'.format('shcol', shcol.__version__)
        for version in ('--version', '-v'):
            result = self._get_stderr_output([version])
            self.assertEqual(result, expected)

    def _test_num_option(self, long_option, short_option):
        option_name = long_option.lstrip('-')
        dummy_item = 'spam'
        for value in ('0', '2', '80', '100'):
            args = self.parser.parse_args([long_option, value, DUMMY_ITEM])
            self.assertEqual(getattr(args, option_name), int(value))
        for invalid in ('-42', '-1', '1.0', 'x'):
            error = self._get_stderr_output([long_option, invalid, DUMMY_ITEM])
            self.assertIn('invalid num value', error)
        args = self.parser.parse_args([short_option, '2', DUMMY_ITEM])
        self.assertEqual(getattr(args, option_name), 2)

    def test_spacing(self):
        self._test_num_option('--spacing', '-s')

    def test_line_width(self):
        self._test_num_option('--width', '-w')

    def test_items(self):
        items = ['spam', 'ham', 'egg']
        args = self.parser.parse_args(items)
        self.assertEqual(args.items, items)

    def test_sort(self):
        args = self.parser.parse_args([DUMMY_ITEM])
        self.assertFalse(args.sort)
        args = self.parser.parse_args(['--sort', DUMMY_ITEM])
        self.assertTrue(args.sort)

@@ -126,27 +126,424 @@
 IO%0A%0A
-DUMMY_ITEM = 'spam'
+class CapturedStream(object):%0A    def __init__(self, stream_name):%0A        self.stream_name = stream_name%0A        self.original_stream = getattr(sys, stream_name)%0A        self.pseudo_stream = StringIO()%0A%0A    def __enter__(self):%0A        setattr(sys, self.stream_name, self.pseudo_stream)%0A        return self.pseudo_stream%0A%0A    def __exit__(self, *unused):%0A        setattr(sys, self.stream_name, self.original_stream)
 %0A%0Acl
@@ -581,24 +581,49 @@
 .TestCase):%0A
+    dummy_item = 'spam'%0A%0A
     def setU
@@ -759,84 +759,76 @@
     
-old_stderr = sys.stderr%0A        sys.stderr = StringIO()%0A        result = ''%0A
+result = ''%0A        with CapturedStream('stderr') as errstream:%0A    
     
@@ -832,24 +832,28 @@
        try:%0A
+    
             
@@ -889,16 +889,20 @@
         
+   
+ 
 except S
@@ -916,32 +916,36 @@
 it:%0A            
+    
 result = sys.std
@@ -937,26 +937,25 @@
 esult = 
-sys.stderr
+errstream
 .getvalu
@@ -975,61 +975,8 @@
 n')%0A
-        finally:%0A            sys.stderr = old_stderr%0A
     
@@ -1474,26 +1474,31 @@
  value, 
-DUMMY_ITEM
+self.dummy_item
 %5D)%0A     
@@ -1655,16 +1655,33 @@
 _output(
+%0A                
 %5Blong_op
@@ -1695,27 +1695,45 @@
 nvalid, 
-DUMMY_ITEM%5D
+self.dummy_item%5D%0A            
 )%0A      
@@ -1838,26 +1838,31 @@
 n, '2', 
-DUMMY_ITEM
+self.dummy_item
 %5D)%0A     
@@ -2087,16 +2087,20 @@
 est_item
+_arg
 s(self):
@@ -2136,16 +2136,17 @@
 m', 'egg
+s
 '%5D%0A     
@@ -2230,16 +2230,374 @@
 items)%0A%0A
+    def test_no_item_args(self):%0A        with CapturedStream('stdin') as instream:%0A            args = self.parser.parse_args(%5B%5D)%0A            self.assertFalse(args.items)%0A            instream.write('spam%5Cnham%5Cneggs')%0A            instream.seek(0)%0A            args = self.parser.parse_args(%5B%5D)%0A            self.assertEqual(args.items, %5B'spam', 'ham', 'eggs'%5D)%0A%0A
     def 
@@ -2652,26 +2652,31 @@
 e_args(%5B
-DUMMY_ITEM
+self.dummy_item
 %5D)%0A     
@@ -2759,18 +2759,23 @@
 t', 
-DUMMY_ITEM
+self.dummy_item
 %5D)%0A 

Add CLI-test for passing items via stdin.