Skip to content

Commit

Permalink
Implement option --targets to list selected files. See #47
Browse files Browse the repository at this point in the history
  • Loading branch information
svent committed Jan 4, 2016
1 parent fc60949 commit a29f6c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
5 changes: 5 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Options struct {
ShowLineNumbers bool `short:"n" long:"line-number" description:"show line numbers (default: off)"`
NoShowLineNumbers func() `short:"N" long:"no-line-number" description:"do not show line numbers" json:"-"`
Stats bool `long:"stats" description:"show statistics"`
TargetsOnly bool `long:"targets" description:"only list selected files, do not search"`
ListTypes func() `long:"list-types" description:"list available file types" json:"-" default-mask:"-"`
Version func() `short:"V" long:"version" description:"show version and license information" json:"-"`
WriteConfig bool `long:"write-config" description:"save config for loaded configs + given command line arguments" json:"-"`
Expand Down Expand Up @@ -539,6 +540,10 @@ func (o *Options) checkCompatibility(targets []string) error {
return errors.New("context options are not supported when reading from STDIN or network")
}

if (stdinTargetFound || netTargetFound) && o.TargetsOnly {
return errors.New("targets option not supported when reading from STDIN or network")
}

if (o.ContextBefore != 0 || o.ContextAfter != 0) && (o.Count || o.FilesWithMatches || o.FilesWithoutMatch) {
return errors.New("context options cannot be combined with count or list option")
}
Expand Down
14 changes: 10 additions & 4 deletions sift.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ func processFileTargets() {
var infile *os.File
var reader io.Reader

if options.TargetsOnly {
fmt.Println(filepath)
continue
}

if filepath == "-" {
infile = os.Stdin
} else {
Expand Down Expand Up @@ -557,8 +562,9 @@ func main() {
parser := flags.NewNamedParser("sift", flags.HelpFlag|flags.PassDoubleDash)
parser.AddGroup("Options", "Options", &options)
parser.Name = "sift"
parser.Usage = "[OPTIONS] PATTERN [FILE|PATH|tcp://HOST:PORT...]\n" +
" sift [OPTIONS] [-e PATTERN | -f FILE] [FILE|PATH|tcp://HOST:PORT...]"
parser.Usage = "[OPTIONS] PATTERN [FILE|PATH|tcp://HOST:PORT]...\n" +
" sift [OPTIONS] [-e PATTERN | -f FILE] [FILE|PATH|tcp://HOST:PORT]...\n" +
" sift [OPTIONS] --targets [FILE|PATH]..."

// temporarily parse options to see if the --no-conf option was used and
// then discard the result
Expand Down Expand Up @@ -604,10 +610,10 @@ func main() {
}
}
if len(global.matchPatterns) == 0 {
if len(args) == 0 && !(options.PrintConfig || options.WriteConfig) {
if len(args) == 0 && !(options.PrintConfig || options.WriteConfig || options.TargetsOnly) {
errorLogger.Fatalln("No pattern given. Try 'sift --help' for more information.")
}
if len(args) > 0 {
if len(args) > 0 && !options.TargetsOnly {
global.matchPatterns = append(global.matchPatterns, args[0])
args = args[1:len(args)]
}
Expand Down

0 comments on commit a29f6c8

Please sign in to comment.