<commit_before>package main

import (
	crand "crypto/rand"
	"encoding/hex"
	"errors"
	"flag"
	"github.com/fzzy/radix/redis"
	"io"
	"log"
	"math/rand"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"syscall"
	"time"
	"fmt"
)

const (
	DefaultExpires                  = 86400
	ExitCodeRedisDead               = 1
	ExitCodeRedisUnsupportedVersion = 2
	ExitCodeCannotGetLock           = 3
	ExitCodePanic                   = 4
	UnlockLUAScript                 = "if redis.call(\"get\",KEYS[1]) == ARGV[1]\nthen\nreturn redis.call(\"del\",KEYS[1])\nelse\nreturn 0\nend\n"
)

type Options struct {
	Redis    string
	Expires  int
	Keep     bool
	Wait     bool
	ExitCode int
}

func main() {
	code := run()
	os.Exit(code)
}

func parseOptions() (opt *Options, key string, program string, args []string) {
	var redis string
	var expires int
	var keep bool
	var noDelay bool
	var delay bool
	var exitZero bool
	var exitNonZero bool

	flag.Usage = usage
	flag.StringVar(&redis, "redis", "127.0.0.1:6379", "redis-server host:port")
	flag.IntVar(&expires, "expires", DefaultExpires, "The lock will be auto-released after the expire time is reached.")
	flag.BoolVar(&keep, "keep", false, "Keep the lock after invoked command exited.")
	flag.BoolVar(&noDelay, "n", false, "No delay. If KEY is locked by another process, redis-setlock gives up.")
	flag.BoolVar(&delay, "N", true, "(Default.) Delay. If KEY is locked by another process, redis-setlock waits until it can obtain a new lock.")
	flag.BoolVar(&exitZero, "x", false, "If KEY is locked, redis-setlock exits zero.")
	flag.BoolVar(&exitNonZero, "X", true, "(Default.) If KEY is locked, redis-setlock prints an error message and exits nonzero.")
	flag.Parse()

	opt = &Options{
		Redis:    redis,
		Keep:     keep,
		Wait:     true,
		ExitCode: ExitCodeCannotGetLock,
		Expires:  expires,
	}
	if noDelay {
		opt.Wait = false
	}
	if exitZero {
		opt.ExitCode = 0
	}

	remainArgs := flag.Args()
	if len(remainArgs) >= 2 {
		key = remainArgs[0]
		program = remainArgs[1]
		if len(remainArgs) >= 3 {
			args = remainArgs[2:]<commit_msg>add version flag<commit_after>package main

import (
	crand "crypto/rand"
	"encoding/hex"
	"errors"
	"flag"
	"github.com/fzzy/radix/redis"
	"io"
	"log"
	"math/rand"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"syscall"
	"time"
	"fmt"
)

const (
	DefaultExpires                  = 86400
	ExitCodeRedisDead               = 1
	ExitCodeRedisUnsupportedVersion = 2
	ExitCodeCannotGetLock           = 3
	ExitCodePanic                   = 4
	UnlockLUAScript                 = "if redis.call(\"get\",KEYS[1]) == ARGV[1]\nthen\nreturn redis.call(\"del\",KEYS[1])\nelse\nreturn 0\nend\n"
	Version                         = "0.0.1"
)

type Options struct {
	Redis    string
	Expires  int
	Keep     bool
	Wait     bool
	ExitCode int
}

func main() {
	code := run()
	os.Exit(code)
}

func parseOptions() (opt *Options, key string, program string, args []string) {
	var redis string
	var expires int
	var keep bool
	var noDelay bool
	var delay bool
	var exitZero bool
	var exitNonZero bool
	var showVersion bool

	flag.Usage = usage
	flag.StringVar(&redis, "redis", "127.0.0.1:6379", "redis-server host:port")
	flag.IntVar(&expires, "expires", DefaultExpires, "The lock will be auto-released after the expire time is reached.")
	flag.BoolVar(&keep, "keep", false, "Keep the lock after invoked command exited.")
	flag.BoolVar(&noDelay, "n", false, "No delay. If KEY is locked by another process, redis-setlock gives up.")
	flag.BoolVar(&delay, "N", true, "(Default.) Delay. If KEY is locked by another process, redis-setlock waits until it can obtain a new lock.")
	flag.BoolVar(&exitZero, "x", false, "If KEY is locked, redis-setlock exits zero.")
	flag.BoolVar(&exitNonZero, "X", true, "(Default.) If KEY is locked, redis-setlock prints an error message and exits nonzero.")
	flag.BoolVar(&showVersion, "version", false, fmt.Sprintf("version %s", Version))
	flag.Parse()

	if showVersion {
		fmt.Fprintf(os.Stderr, "version: %s\n", Version)
		os.Exit(0)
	}

	opt = &Options{
		Redis:    redis,
		Keep:     keep,
		Wait:     true,
		ExitCode: ExitCodeCannotGetLock,
		Expires:  expires,
	}
	if noDelay {
		opt.Wait = false
	}
	if exitZero {
		opt.ExitCode = 0
	}

	remainArgs := flag.Args()
	if len(remainArgs) >= 2 {
		key = remainArgs[0]
		program = remainArgs[1]
		if len(remainArgs) >= 3 {
			args = remainArgs[2:]
{'fraction_non_alphanumeric': 0.13084326306141156, 'fraction_numerical': 0.014207149404216315, 'mean_word_length': 3.254385964912281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4747105', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1572, 'n_words': 445}