Example — moke 1.2.0 documentation

# Example

## Stand-alone command line apps

If for some reason you do not want call `moke` to execute your mokefile it is possible to rename it and call it directly.:

    chmod +x mokefile.py
    mv mokefile.py myapp.py
    ./myapp.py <task>

This requires shebang line:

    #!/usr/bin/env python3

If your `mokefile.py` contains only a single task call the function `main` and you will be able to omit `<task>` from the call:

    ./myapp.py

The tiny `grop.py` example included in the test-suite is a tiny replacement for `grep`.

    #!/usr/bin/env python3
    from moke import *
    import re

    @task
    def main(pattern, i=stdin, o=stdout, only_matching=False):
        """grop is not grep.
        """
        sre = re.compile(pattern)
        with i as i, o as o:
            for line in i:
                match = sre.match(line)
                if match:
                    if only_matching:
                        o.write("%s\n" % match.groups()[0])
                    else:
                        o.write(line)

    if __name__ == "__main__":
        task()

Example usage (in the `test` directory):

    cat data/grop.inp | scripts/grop.py ".*\(\d{2}\).*"'