using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using Foodsoft.Alpm; // ReSharper disable once UnusedType.Global namespace Samples { [Example("installed")] internal class Installed : IExample { public int Run(string[] args) { using var h = new Handle("/", "/var/lib/pacman"); using var db = h.LocalDB; foreach (var pkg in db.PackageCache) using (pkg) { Console.WriteLine("{0} {1}", pkg.Name, pkg.Version); } return 0; } } [Example("search")] internal class Search : IExample { public int Run(string[] args) { using var h = new Handle("/", "/var/lib/pacman"); using var db = h.RegisterSyncDB("core"); db.Servers = new[]{"http://www.google.com"}; db.Update(true); foreach (var pkg in db.PackageCache) using (pkg) { Console.WriteLine("{0} {1} {2}", pkg.Name, pkg.Version, pkg.Description); } return 0; } } } namespace Samples { internal static class Program { private static int Main(string[] args) { var examples = (from t in Assembly.GetExecutingAssembly().GetTypes() let attribute = (Example?) t.GetCustomAttribute(typeof(Example)) where attribute != null select new {Name = attribute.Name, Type = t}).ToImmutableDictionary((e) => e.Name); if (args.Length < 1) { Console.Error.WriteLine("Sample"); foreach (var example in examples.Values) { Console.Error.WriteLine("{0}", example.Name); } return 1; } int ret; try { ret = ((IExample) Activator.CreateInstance(examples[args[0]].Type)!).Run(args); } catch (AlpmException e) { Console.Error.WriteLine("{0}", e.Message); return 1; } return ret; } } [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)] public class Example : Attribute { public string Name { get; } public Example(string name) { Name = name; } } public interface IExample { public int Run(string[] args); } }