This tutorial will be all about writing a simple grep clone in arc.
When completed our grep clone will be able to:
- Accept a
PATTERNto search for
- Accept any number of
- Ouptut lines of the
FILESthat match the
PATTERNto stdout, with coloring
To begin with we need to scaffold the project
Install arc if you haven't already
And create a file called
grep_clone.py that contains the following:
Next, we're going to want to add arguments to the command. For now, we'll only be implementing the
FILES arguments. All of grep's many flags will be left alone for now.
As you can see, we've already got a validated regex pattern, and file handles to each of the specified files.
With type handling / data validation already out of the way, the implentation will be fairly straightfoward.
import re import arc from arc import color from arc.types import File @arc.command() def grep(pattern: re.Pattern, files: list[File.Read]): for file in files: # Iterate over all the files for line in file.readlines(): # Iterate over all the line in the file if match := pattern.search(line): # check for a match # If there is a match, highlight it colored = pattern.sub( color.fg.RED + match.group() + color.effects.CLEAR, line, ) arc.print(colored, end="") grep()
$ python grep.py arc docs/index.md docs/usage/intro.md *arc* is a tool for building declartive, and highly extendable CLI applications for Python ^3.10 based on Python type-hints *arc's* features include: *arc* can be installed with pip $ pip install arc-cli [source repo](https://github.com/seanrcollings/arc/tree/master/docs/examples) Pretty cool, huh? *arc* will pull *all* of the information that it needs from your [function docstrings and parameter definitons.](usage/documentation-generation.md) *arc* is easily configurable via the `#!python arc.configure()` function. *View the [reference](reference/config.md#arc.config.configure) for details on all the configuration options* This intro serves as a quick starting off point to see some of *arc's* most useful features. The simplest *arc* program would look like this 1. `#!python @arc.command()` is a Python decorator that transforms a function into an *arc* command. *arc* uses argument type hints for data validation / conversion. For example, say we want to write a command that can sum two numbers together: if the input fails to validate, *arc* will report a user-friendly error for the type - if a parameter does not specify a type, *arc* implicitly types it as `#!python str`. - **All** builtin types are supported by *arc*, and many stdlib types