🕷 zenspider.com

by ryan davis

Looking for the Ruby Quickref?

to the archives!

Introducing path_expander

Published at 2016-05-14 12:00:00 -0700

I released a new project recently called path_expander. It’s been a long time coming and it removes a number of copy-pastes I have across several of my projects and will be used to expand the capabilities and utility of several more.

PathExpander helps pre-process command-line arguments expanding directories into their constituent files. It further helps by providing additional mechanisms to make specifying subsets easier with path subtraction and allowing for command-line arguments to be saved in a file.

NOTE: This is NOT an options processor. It is a path processor (basically everything else besides options). It does provide a mechanism for pre-filtering cmdline options, but not with the intent of actually processing them in PathExpander. Use OptionParser to deal with options either before or after passing ARGV through PathExpander.

I’ve needed this for sooo long to clean up, extend, and unify the myriad of file processing tools that I’ve written over the years.

Basic Usage

So what can it do? With simple code like this:

  PathExpander.new(ARGV, "**/*.rb").process.each do |f|
    # do something with each file

You can then use it like this:

% mytool lib

to pick up all .rb files under lib.


Not that interesting? Absolutely true. But then you can do this:

% mytool lib -lib/ignore_this_file.rb


% mytool lib -lib/ignore_this_subdir

in order to process all ruby files under lib minus some subset of files.

Persistent Args

And here is where it gets interesting, because you can read in files as args:

% echo lib                      >> args.txt
% echo -lib/ignore_this_subdir  >> args.txt
% echo -lib/ignore_this_file.rb >> args.txt
% mytool @args.txt

This intermixes with regular args, so it is easy to persist the common args and then continue experimenting on the commandline.

I love this and it will make my life so much easier.

Ignore Files

It also has basic capabilities to process and filter globs via a sort of .ignore file. It’s basically a simpler version of .gitignore. It doesn’t do as much, but it is also much simpler to implement, so it strikes a nice balance.

Basically it reads a file for patterns and uses File.fnmatch to see if any paths it is passed match those patterns and should be ignored. It is pretty simple at this point, but I can see it being extended into a full-fledged filter.

Other Features?

That’s about all it does up front… it is designed to be subclassed in order to make method over-riding easy to do. This also helps you keep project specific things like file glob strings in easily namespaced constants. I’m already using this in minitest-sprint to pre-filter some options in order to pass them off to ruby directly instead of OptionProcessor.

If you write tools that process files, I hope you can get some utility from path_expander. Please file an issue if you have any questions or ideas to share about it.