Вот простой пример, который может вам помочь:
from cmd import Cmd
class PipelineExample(Cmd):
def do_greet(self, person):
if person:
greeting = "hello, " + person
else:
greeting = 'hello'
self.output = greeting
def do_echo(self, text):
self.output = text
def do_pipe(self, args):
buffer = None
for arg in args:
s = arg
if buffer:
# This command just adds the output of a previous command as the last argument
s += ' ' + buffer
self.onecmd(s)
buffer = self.output
def postcmd(self, stop, line):
if hasattr(self, 'output') and self.output:
print self.output
self.output = None
return stop
def parseline(self, line):
if '|' in line:
return 'pipe', line.split('|'), line
return Cmd.parseline(self, line)
def do_EOF(self, line):
return True
if __name__ == '__main__':
PipelineExample().cmdloop()
Вот пример сеанса:
(Cmd) greet wong
hello, wong
(Cmd) echo wong | greet
hello, wong
(Cmd) echo wong | greet | greet
hello, hello, wong